blob: c054438889e96df4b0368d29bf65539bef510665 [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
Mayank Ranaa99689a2016-08-10 17:39:47 -070025#include <linux/ratelimit.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030026#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/list.h>
29#include <linux/dma-mapping.h>
30
31#include <linux/usb/ch9.h>
Mayank Ranaa99689a2016-08-10 17:39:47 -070032#include <linux/usb/composite.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030033#include <linux/usb/gadget.h>
34
Felipe Balbi80977dc2014-08-19 16:37:22 -050035#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030036#include "core.h"
37#include "gadget.h"
38#include "io.h"
39
Mayank Ranaa99689a2016-08-10 17:39:47 -070040static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup);
41static int dwc3_gadget_wakeup_int(struct dwc3 *dwc);
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020042/**
43 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
44 * @dwc: pointer to our context structure
45 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
46 *
47 * Caller should take care of locking. This function will
48 * return 0 on success or -EINVAL if wrong Test Selector
49 * is passed
50 */
51int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
52{
53 u32 reg;
54
55 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
56 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
57
58 switch (mode) {
59 case TEST_J:
60 case TEST_K:
61 case TEST_SE0_NAK:
62 case TEST_PACKET:
63 case TEST_FORCE_EN:
64 reg |= mode << 1;
65 break;
66 default:
67 return -EINVAL;
68 }
69
70 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
71
72 return 0;
73}
74
Felipe Balbi8598bde2012-01-02 18:55:57 +020075/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030076 * dwc3_gadget_get_link_state - Gets current state of USB Link
77 * @dwc: pointer to our context structure
78 *
79 * Caller should take care of locking. This function will
80 * return the link state on success (>= 0) or -ETIMEDOUT.
81 */
82int dwc3_gadget_get_link_state(struct dwc3 *dwc)
83{
84 u32 reg;
85
86 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
87
88 return DWC3_DSTS_USBLNKST(reg);
89}
90
91/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020092 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
93 * @dwc: pointer to our context structure
94 * @state: the state to put link into
95 *
96 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 */
99int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
100{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800101 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200102 u32 reg;
103
Paul Zimmerman802fde92012-04-27 13:10:52 +0300104 /*
105 * Wait until device controller is ready. Only applies to 1.94a and
106 * later RTL.
107 */
108 if (dwc->revision >= DWC3_REVISION_194A) {
109 while (--retries) {
110 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
111 if (reg & DWC3_DSTS_DCNRD)
112 udelay(5);
113 else
114 break;
115 }
116
117 if (retries <= 0)
118 return -ETIMEDOUT;
119 }
120
Felipe Balbi8598bde2012-01-02 18:55:57 +0200121 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
122 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
123
124 /* set requested state */
125 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
126 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
127
Paul Zimmerman802fde92012-04-27 13:10:52 +0300128 /*
129 * The following code is racy when called from dwc3_gadget_wakeup,
130 * and is not needed, at least on newer versions
131 */
132 if (dwc->revision >= DWC3_REVISION_194A)
133 return 0;
134
Felipe Balbi8598bde2012-01-02 18:55:57 +0200135 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300136 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200137 while (--retries) {
138 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
139
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 if (DWC3_DSTS_USBLNKST(reg) == state)
141 return 0;
142
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800143 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200144 }
145
Felipe Balbi73815282015-01-27 13:48:14 -0600146 dwc3_trace(trace_dwc3_gadget,
147 "link state change request timed out");
Felipe Balbi8598bde2012-01-02 18:55:57 +0200148
149 return -ETIMEDOUT;
150}
151
John Youndca01192016-05-19 17:26:05 -0700152/**
153 * dwc3_ep_inc_trb() - Increment a TRB index.
154 * @index - Pointer to the TRB index to increment.
155 *
156 * The index should never point to the link TRB. After incrementing,
157 * if it is point to the link TRB, wrap around to the beginning. The
158 * link TRB is always at the last TRB entry.
159 */
160static void dwc3_ep_inc_trb(u8 *index)
161{
162 (*index)++;
163 if (*index == (DWC3_TRB_NUM - 1))
164 *index = 0;
165}
166
Felipe Balbief966b92016-04-05 13:09:51 +0300167static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200168{
John Youndca01192016-05-19 17:26:05 -0700169 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300170}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200171
Felipe Balbief966b92016-04-05 13:09:51 +0300172static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
173{
John Youndca01192016-05-19 17:26:05 -0700174 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200175}
176
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 */
198int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
199{
200 int last_fifo_depth = 0;
201 int ram1_depth;
202 int fifo_size;
203 int mdwidth;
204 int num;
205
206 if (!dwc->needs_fifo_resize)
207 return 0;
208
209 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
210 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
211
212 /* MDWIDTH is represented in bits, we need it in bytes */
213 mdwidth >>= 3;
214
215 /*
216 * FIXME For now we will only allocate 1 wMaxPacketSize space
217 * for each enabled endpoint, later patches will come to
218 * improve this algorithm so that we better use the internal
219 * FIFO space
220 */
221 for (num = 0; num < dwc->num_in_eps; num++) {
222 /* bit0 indicates direction; 1 means IN ep */
223 struct dwc3_ep *dep = dwc->eps[(num << 1) | 1];
224 int mult = 1;
225 int tmp;
226
227 if (!(dep->flags & DWC3_EP_ENABLED))
228 continue;
229
230 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
231 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
232 mult = 3;
233
234 /*
235 * REVISIT: the following assumes we will always have enough
236 * space available on the FIFO RAM for all possible use cases.
237 * Make sure that's true somehow and change FIFO allocation
238 * accordingly.
239 *
240 * If we have Bulk or Isochronous endpoints, we want
241 * them to be able to be very, very fast. So we're giving
242 * those endpoints a fifo_size which is enough for 3 full
243 * packets
244 */
245 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
246 tmp += mdwidth;
247
248 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
249
250 fifo_size |= (last_fifo_depth << 16);
251
252 dwc3_trace(trace_dwc3_gadget, "%s: Fifo Addr %04x Size %d",
253 dep->name, last_fifo_depth, fifo_size & 0xffff);
254
255 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num), fifo_size);
256
257 last_fifo_depth += (fifo_size & 0xffff);
258 }
259
260 return 0;
261}
262
Felipe Balbi72246da2011-08-19 18:10:58 +0300263void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
264 int status)
265{
266 struct dwc3 *dwc = dep->dwc;
267
Felipe Balbi737f1ae2016-08-11 12:24:27 +0300268 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300269 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200270 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300271
272 if (req->request.status == -EINPROGRESS)
273 req->request.status = status;
274
Pratyush Anand0416e492012-08-10 13:42:16 +0530275 if (dwc->ep0_bounced && dep->number == 0)
276 dwc->ep0_bounced = false;
277 else
278 usb_gadget_unmap_request(&dwc->gadget, &req->request,
279 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300280
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500281 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300282
283 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200284 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300285 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300286
287 if (dep->number > 1)
288 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300289}
290
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500291int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300292{
293 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300294 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300295 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300296 u32 reg;
297
298 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
299 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
300
301 do {
302 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
303 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300304 status = DWC3_DGCMD_STATUS(reg);
305 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300306 ret = -EINVAL;
307 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300308 }
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300309 } while (timeout--);
310
311 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300312 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300313 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300314 }
315
Felipe Balbi71f7e702016-05-23 14:16:19 +0300316 trace_dwc3_gadget_generic_cmd(cmd, param, status);
317
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300318 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300319}
320
Felipe Balbic36d8e92016-04-04 12:46:33 +0300321static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
322
Felipe Balbi2cd47182016-04-12 16:42:43 +0300323int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
324 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300325{
Felipe Balbi2cd47182016-04-12 16:42:43 +0300326 struct dwc3 *dwc = dep->dwc;
Hemant Kumar43874172016-08-25 16:17:48 -0700327 u32 timeout = 3000;
Felipe Balbi72246da2011-08-19 18:10:58 +0300328 u32 reg;
329
Felipe Balbi0933df12016-05-23 14:02:33 +0300330 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300331 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300332 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300333
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300334 /*
335 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
336 * we're issuing an endpoint command, we must check if
337 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
338 *
339 * We will also set SUSPHY bit to what it was before returning as stated
340 * by the same section on Synopsys databook.
341 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300342 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
343 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
344 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
345 susphy = true;
346 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
347 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
348 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300349 }
350
Felipe Balbic36d8e92016-04-04 12:46:33 +0300351 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
352 int needs_wakeup;
353
354 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
355 dwc->link_state == DWC3_LINK_STATE_U2 ||
356 dwc->link_state == DWC3_LINK_STATE_U3);
357
358 if (unlikely(needs_wakeup)) {
359 ret = __dwc3_gadget_wakeup(dwc);
360 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
361 ret);
362 }
363 }
364
Felipe Balbi2eb88012016-04-12 16:53:39 +0300365 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
366 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
367 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300368
Felipe Balbi2eb88012016-04-12 16:53:39 +0300369 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300370 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300371 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300372 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300373 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000374
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000375 switch (cmd_status) {
376 case 0:
377 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300378 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000379 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000380 ret = -EINVAL;
381 break;
382 case DEPEVT_TRANSFER_BUS_EXPIRY:
383 /*
384 * SW issues START TRANSFER command to
385 * isochronous ep with future frame interval. If
386 * future interval time has already passed when
387 * core receives the command, it will respond
388 * with an error status of 'Bus Expiry'.
389 *
390 * Instead of always returning -EINVAL, let's
391 * give a hint to the gadget driver that this is
392 * the case by returning -EAGAIN.
393 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000394 ret = -EAGAIN;
395 break;
396 default:
397 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
398 }
399
Felipe Balbic0ca3242016-04-04 09:11:51 +0300400 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300401 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300402 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300403
Felipe Balbif6bb2252016-05-23 13:53:34 +0300404 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300405 ret = -ETIMEDOUT;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700406 dwc3_trace(trace_dwc3_gadget, "Command Timed Out");
407 dev_err(dwc->dev, "%s command timeout for %s\n",
408 dwc3_gadget_ep_cmd_string(cmd), dep->name);
Hemant Kumar43874172016-08-25 16:17:48 -0700409 if (!(cmd & DWC3_DEPCMD_ENDTRANSFER)) {
410 dwc->ep_cmd_timeout_cnt++;
411 dwc3_notify_event(dwc,
412 DWC3_CONTROLLER_RESTART_USB_SESSION);
413 }
Felipe Balbi0933df12016-05-23 14:02:33 +0300414 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300415 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300416
Felipe Balbi0933df12016-05-23 14:02:33 +0300417 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
418
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300419 if (unlikely(susphy)) {
420 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
421 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
422 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
423 }
424
Felipe Balbic0ca3242016-04-04 09:11:51 +0300425 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300426}
427
John Youn50c763f2016-05-31 17:49:56 -0700428static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
429{
430 struct dwc3 *dwc = dep->dwc;
431 struct dwc3_gadget_ep_cmd_params params;
432 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
433
434 /*
435 * As of core revision 2.60a the recommended programming model
436 * is to set the ClearPendIN bit when issuing a Clear Stall EP
437 * command for IN endpoints. This is to prevent an issue where
438 * some (non-compliant) hosts may not send ACK TPs for pending
439 * IN transfers due to a mishandled error condition. Synopsys
440 * STAR 9000614252.
441 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800442 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
443 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700444 cmd |= DWC3_DEPCMD_CLEARPENDIN;
445
446 memset(&params, 0, sizeof(params));
447
Felipe Balbi2cd47182016-04-12 16:42:43 +0300448 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700449}
450
Felipe Balbi72246da2011-08-19 18:10:58 +0300451static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
452{
453 struct dwc3 *dwc = dep->dwc;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700454 u32 num_trbs = DWC3_TRB_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300455
456 if (dep->trb_pool)
457 return 0;
458
Mayank Ranaa99689a2016-08-10 17:39:47 -0700459 dep->trb_pool = dma_zalloc_coherent(dwc->dev,
460 sizeof(struct dwc3_trb) * num_trbs,
Felipe Balbi72246da2011-08-19 18:10:58 +0300461 &dep->trb_pool_dma, GFP_KERNEL);
462 if (!dep->trb_pool) {
463 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
464 dep->name);
465 return -ENOMEM;
466 }
Mayank Ranaa99689a2016-08-10 17:39:47 -0700467 dep->num_trbs = num_trbs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300468
469 return 0;
470}
471
472static void dwc3_free_trb_pool(struct dwc3_ep *dep)
473{
474 struct dwc3 *dwc = dep->dwc;
475
Mayank Ranaa99689a2016-08-10 17:39:47 -0700476 /* Freeing of GSI EP TRBs are handled by GSI EP ops. */
477 if (dep->endpoint.ep_type == EP_TYPE_GSI)
478 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300479
Mayank Rana4dd882c2016-10-05 09:43:05 -0700480 /*
481 * Clean up ep ring to avoid getting xferInProgress due to stale trbs
482 * with HWO bit set from previous composition when update transfer cmd
483 * is issued.
484 */
485 if (dep->number > 1 && dep->trb_pool && dep->trb_pool_dma) {
486 memset(&dep->trb_pool[0], 0,
487 sizeof(struct dwc3_trb) * dep->num_trbs);
488 dev_dbg(dwc->dev, "Clr_TRB ring of %s\n", dep->name);
489
Mayank Ranaa99689a2016-08-10 17:39:47 -0700490 dma_free_coherent(dwc->dev,
491 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
492 dep->trb_pool, dep->trb_pool_dma);
493 dep->trb_pool = NULL;
494 dep->trb_pool_dma = 0;
495 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300496}
497
John Younc4509602016-02-16 20:10:53 -0800498static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
499
500/**
501 * dwc3_gadget_start_config - Configure EP resources
502 * @dwc: pointer to our controller context structure
503 * @dep: endpoint that is being enabled
504 *
505 * The assignment of transfer resources cannot perfectly follow the
506 * data book due to the fact that the controller driver does not have
507 * all knowledge of the configuration in advance. It is given this
508 * information piecemeal by the composite gadget framework after every
509 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
510 * programming model in this scenario can cause errors. For two
511 * reasons:
512 *
513 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
514 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
515 * multiple interfaces.
516 *
517 * 2) The databook does not mention doing more DEPXFERCFG for new
518 * endpoint on alt setting (8.1.6).
519 *
520 * The following simplified method is used instead:
521 *
522 * All hardware endpoints can be assigned a transfer resource and this
523 * setting will stay persistent until either a core reset or
524 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
525 * do DEPXFERCFG for every hardware endpoint as well. We are
526 * guaranteed that there are as many transfer resources as endpoints.
527 *
528 * This function is called for each endpoint when it is being enabled
529 * but is triggered only when called for EP0-out, which always happens
530 * first, and which should only happen in one of the above conditions.
531 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300532static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
533{
534 struct dwc3_gadget_ep_cmd_params params;
535 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800536 int i;
537 int ret;
538
539 if (dep->number)
540 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300541
542 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800543 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544
Felipe Balbi2cd47182016-04-12 16:42:43 +0300545 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800546 if (ret)
547 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300548
John Younc4509602016-02-16 20:10:53 -0800549 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
550 struct dwc3_ep *dep = dwc->eps[i];
551
552 if (!dep)
553 continue;
554
555 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
556 if (ret)
557 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300558 }
559
560 return 0;
561}
562
563static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200564 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300565 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300566 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300567{
568 struct dwc3_gadget_ep_cmd_params params;
569
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300570 if (dev_WARN_ONCE(dwc->dev, modify && restore,
571 "Can't modify and restore\n"))
572 return -EINVAL;
573
Felipe Balbi72246da2011-08-19 18:10:58 +0300574 memset(&params, 0x00, sizeof(params));
575
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300576 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900577 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
578
579 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800580 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300581 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300582 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900583 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300584
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300585 if (modify) {
586 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
587 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600588 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
589 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300590 } else {
591 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600592 }
593
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300594 if (usb_endpoint_xfer_control(desc))
595 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300596
597 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
598 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300599
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200600 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300601 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
602 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300603 dep->stream_capable = true;
604 }
605
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500606 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300607 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300608
609 /*
610 * We are doing 1:1 mapping for endpoints, meaning
611 * Physical Endpoints 2 maps to Logical Endpoint 2 and
612 * so on. We consider the direction bit as part of the physical
613 * endpoint number. So USB endpoint 0x81 is 0x03.
614 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300615 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300616
617 /*
618 * We must use the lower 16 TX FIFOs even though
619 * HW might have more
620 */
621 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300622 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300623
624 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300625 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300626 dep->interval = 1 << (desc->bInterval - 1);
627 }
628
Felipe Balbi2cd47182016-04-12 16:42:43 +0300629 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300630}
631
632static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
633{
634 struct dwc3_gadget_ep_cmd_params params;
635
636 memset(&params, 0x00, sizeof(params));
637
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300638 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300639
Felipe Balbi2cd47182016-04-12 16:42:43 +0300640 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
641 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300642}
643
644/**
645 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
646 * @dep: endpoint to be initialized
647 * @desc: USB Endpoint Descriptor
648 *
649 * Caller should take care of locking
650 */
651static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200652 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300653 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300654 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300655{
656 struct dwc3 *dwc = dep->dwc;
657 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300658 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300659
Felipe Balbi73815282015-01-27 13:48:14 -0600660 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300661
Felipe Balbi72246da2011-08-19 18:10:58 +0300662 if (!(dep->flags & DWC3_EP_ENABLED)) {
663 ret = dwc3_gadget_start_config(dwc, dep);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700664 if (ret) {
665 dev_err(dwc->dev, "start_config() failed for %s\n",
666 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300667 return ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700668 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300669 }
670
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300671 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, modify,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600672 restore);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700673 if (ret) {
674 dev_err(dwc->dev, "set_ep_config() failed for %s\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300675 return ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700676 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300677
678 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200679 struct dwc3_trb *trb_st_hw;
680 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300681
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200682 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200683 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300684 dep->type = usb_endpoint_type(desc);
685 dep->flags |= DWC3_EP_ENABLED;
686
687 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
688 reg |= DWC3_DALEPENA_EP(dep->number);
689 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
690
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300691 if (usb_endpoint_xfer_control(desc))
Felipe Balbi7ab373a2016-05-23 11:27:26 +0300692 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300693
John Youn0d257442016-05-19 17:26:08 -0700694 /* Initialize the TRB ring */
695 dep->trb_dequeue = 0;
696 dep->trb_enqueue = 0;
697 memset(dep->trb_pool, 0,
698 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
699
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300700 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300701 trb_st_hw = &dep->trb_pool[0];
702
Felipe Balbif6bafc62012-02-06 11:04:53 +0200703 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200704 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
705 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
706 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
707 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300708 }
709
710 return 0;
711}
712
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200713static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300714{
715 struct dwc3_request *req;
716
Felipe Balbi0e146022016-06-21 10:32:02 +0300717 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300718
Felipe Balbi0e146022016-06-21 10:32:02 +0300719 /* - giveback all requests to gadget driver */
720 while (!list_empty(&dep->started_list)) {
721 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200722
Felipe Balbi0e146022016-06-21 10:32:02 +0300723 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200724 }
725
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200726 while (!list_empty(&dep->pending_list)) {
727 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300728
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200729 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300730 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300731}
732
733/**
734 * __dwc3_gadget_ep_disable - Disables a HW endpoint
735 * @dep: the endpoint to disable
736 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200737 * This function also removes requests which are currently processed ny the
738 * hardware and those which are not yet scheduled.
739 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300740 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300741static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
742{
743 struct dwc3 *dwc = dep->dwc;
744 u32 reg;
745
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500746 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
747
Mayank Ranaa99689a2016-08-10 17:39:47 -0700748 if (dep->endpoint.ep_type == EP_TYPE_NORMAL)
749 dwc3_remove_requests(dwc, dep);
750 else if (dep->endpoint.ep_type == EP_TYPE_GSI)
751 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300752
Felipe Balbi687ef982014-04-16 10:30:33 -0500753 /* make sure HW endpoint isn't stalled */
754 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500755 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500756
Felipe Balbi72246da2011-08-19 18:10:58 +0300757 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
758 reg &= ~DWC3_DALEPENA_EP(dep->number);
759 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
760
Felipe Balbi879631a2011-09-30 10:58:47 +0300761 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200762 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200763 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300764 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300765 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300766
Mayank Ranaa99689a2016-08-10 17:39:47 -0700767 /* Keep GSI ep names with "-gsi" suffix */
768 if (!strnstr(dep->name, "gsi", 10)) {
769 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
770 dep->number >> 1,
771 (dep->number & 1) ? "in" : "out");
772 }
773
Felipe Balbi72246da2011-08-19 18:10:58 +0300774 return 0;
775}
776
777/* -------------------------------------------------------------------------- */
778
779static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
780 const struct usb_endpoint_descriptor *desc)
781{
782 return -EINVAL;
783}
784
785static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
786{
787 return -EINVAL;
788}
789
790/* -------------------------------------------------------------------------- */
791
792static int dwc3_gadget_ep_enable(struct usb_ep *ep,
793 const struct usb_endpoint_descriptor *desc)
794{
795 struct dwc3_ep *dep;
796 struct dwc3 *dwc;
797 unsigned long flags;
798 int ret;
799
800 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
801 pr_debug("dwc3: invalid parameters\n");
802 return -EINVAL;
803 }
804
805 if (!desc->wMaxPacketSize) {
806 pr_debug("dwc3: missing wMaxPacketSize\n");
807 return -EINVAL;
808 }
809
810 dep = to_dwc3_ep(ep);
811 dwc = dep->dwc;
812
Felipe Balbi95ca9612015-12-10 13:08:20 -0600813 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
814 "%s is already enabled\n",
815 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300816 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300817
Felipe Balbi72246da2011-08-19 18:10:58 +0300818 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600819 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300820 spin_unlock_irqrestore(&dwc->lock, flags);
821
822 return ret;
823}
824
825static int dwc3_gadget_ep_disable(struct usb_ep *ep)
826{
827 struct dwc3_ep *dep;
828 struct dwc3 *dwc;
829 unsigned long flags;
830 int ret;
831
832 if (!ep) {
833 pr_debug("dwc3: invalid parameters\n");
834 return -EINVAL;
835 }
836
837 dep = to_dwc3_ep(ep);
838 dwc = dep->dwc;
839
Felipe Balbi95ca9612015-12-10 13:08:20 -0600840 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
841 "%s is already disabled\n",
842 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300843 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300844
Felipe Balbi72246da2011-08-19 18:10:58 +0300845 spin_lock_irqsave(&dwc->lock, flags);
846 ret = __dwc3_gadget_ep_disable(dep);
847 spin_unlock_irqrestore(&dwc->lock, flags);
848
849 return ret;
850}
851
852static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
853 gfp_t gfp_flags)
854{
855 struct dwc3_request *req;
856 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300857
858 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900859 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300860 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300861
862 req->epnum = dep->number;
863 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300864
Felipe Balbi68d34c82016-05-30 13:34:58 +0300865 dep->allocated_requests++;
866
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500867 trace_dwc3_alloc_request(req);
868
Felipe Balbi72246da2011-08-19 18:10:58 +0300869 return &req->request;
870}
871
872static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
873 struct usb_request *request)
874{
875 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300876 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300877
Felipe Balbi68d34c82016-05-30 13:34:58 +0300878 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500879 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300880 kfree(req);
881}
882
Felipe Balbi2c78c022016-08-12 13:13:10 +0300883static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
884
Felipe Balbic71fc372011-11-22 11:37:34 +0200885/**
886 * dwc3_prepare_one_trb - setup one TRB from one request
887 * @dep: endpoint for which this request is prepared
888 * @req: dwc3_request pointer
889 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200890static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200891 struct dwc3_request *req, dma_addr_t dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300892 unsigned length, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200893{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200894 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200895
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300896 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200897 dep->name, req, (unsigned long long) dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300898 length, chain ? " chain" : "");
Pratyush Anand915e2022013-01-14 15:59:35 +0530899
Felipe Balbi4faf7552016-04-05 13:14:31 +0300900 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200901
Felipe Balbieeb720f2011-11-28 12:46:59 +0200902 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200903 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200904 req->trb = trb;
905 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300906 req->first_trb_index = dep->trb_enqueue;
Felipe Balbia9c3ca52016-10-05 14:24:37 +0300907 dep->queued_requests++;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200908 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200909
Felipe Balbief966b92016-04-05 13:09:51 +0300910 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530911
Felipe Balbif6bafc62012-02-06 11:04:53 +0200912 trb->size = DWC3_TRB_SIZE_LENGTH(length);
913 trb->bpl = lower_32_bits(dma);
914 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200915
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200916 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200917 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200918 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200919 break;
920
921 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530922 if (!node)
923 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
924 else
925 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200926
927 /* always enable Interrupt on Missed ISOC */
928 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200929 break;
930
931 case USB_ENDPOINT_XFER_BULK:
932 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200933 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200934 break;
935 default:
936 /*
937 * This is only possible with faulty memory because we
938 * checked it already :)
939 */
940 BUG();
941 }
942
Felipe Balbica4d44e2016-03-10 13:53:27 +0200943 /* always enable Continue on Short Packet */
944 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600945
Felipe Balbi2c78c022016-08-12 13:13:10 +0300946 if ((!req->request.no_interrupt && !chain) ||
947 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbica4d44e2016-03-10 13:53:27 +0200948 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
949
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530950 if (chain)
951 trb->ctrl |= DWC3_TRB_CTRL_CHN;
952
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200953 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200954 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
955
956 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500957
958 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200959}
960
John Youn361572b2016-05-19 17:26:17 -0700961/**
962 * dwc3_ep_prev_trb() - Returns the previous TRB in the ring
963 * @dep: The endpoint with the TRB ring
964 * @index: The index of the current TRB in the ring
965 *
966 * Returns the TRB prior to the one pointed to by the index. If the
967 * index is 0, we will wrap backwards, skip the link TRB, and return
968 * the one just before that.
969 */
970static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
971{
Felipe Balbi45438a02016-08-11 12:26:59 +0300972 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -0700973
Felipe Balbi45438a02016-08-11 12:26:59 +0300974 if (!tmp)
975 tmp = DWC3_TRB_NUM - 1;
976
977 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -0700978}
979
Felipe Balbic4233572016-05-12 14:08:34 +0300980static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
981{
982 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -0700983 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +0300984
985 /*
986 * If enqueue & dequeue are equal than it is either full or empty.
987 *
988 * One way to know for sure is if the TRB right before us has HWO bit
989 * set or not. If it has, then we're definitely full and can't fit any
990 * more transfers in our ring.
991 */
992 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -0700993 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
994 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
995 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +0300996
997 return DWC3_TRB_NUM - 1;
998 }
999
John Youn9d7aba72016-08-26 18:43:01 -07001000 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -07001001 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -07001002
John Youn9d7aba72016-08-26 18:43:01 -07001003 if (dep->trb_dequeue < dep->trb_enqueue)
1004 trbs_left--;
1005
John Youn32db3d92016-05-19 17:26:12 -07001006 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001007}
1008
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001009static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001010 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001011{
Felipe Balbi1f512112016-08-12 13:17:27 +03001012 struct scatterlist *sg = req->sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001013 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001014 unsigned int length;
1015 dma_addr_t dma;
1016 int i;
1017
Felipe Balbi1f512112016-08-12 13:17:27 +03001018 for_each_sg(sg, s, req->num_pending_sgs, i) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001019 unsigned chain = true;
1020
1021 length = sg_dma_len(s);
1022 dma = sg_dma_address(s);
1023
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001024 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001025 chain = false;
1026
1027 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001028 chain, i);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001029
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001030 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001031 break;
1032 }
1033}
1034
1035static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001036 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001037{
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001038 unsigned int length;
1039 dma_addr_t dma;
1040
1041 dma = req->request.dma;
1042 length = req->request.length;
1043
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001044 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001045 false, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001046}
1047
Felipe Balbi72246da2011-08-19 18:10:58 +03001048/*
1049 * dwc3_prepare_trbs - setup TRBs from requests
1050 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001051 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001052 * The function goes through the requests list and sets up TRBs for the
1053 * transfers. The function returns once there are no more TRBs available or
1054 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001055 */
Felipe Balbic4233572016-05-12 14:08:34 +03001056static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001057{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001058 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001059
1060 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1061
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001062 if (!dwc3_calc_trbs_left(dep))
John Youn89bc8562016-05-19 17:26:10 -07001063 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001064
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001065 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03001066 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001067 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001068 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001069 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001070
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001071 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001072 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001073 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001074}
1075
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001076static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +03001077{
1078 struct dwc3_gadget_ep_cmd_params params;
1079 struct dwc3_request *req;
1080 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001081 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001082 int ret;
1083 u32 cmd;
1084
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001085 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001086
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001087 dwc3_prepare_trbs(dep);
1088 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001089 if (!req) {
1090 dep->flags |= DWC3_EP_PENDING_REQUEST;
1091 return 0;
1092 }
1093
1094 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001095
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001096 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301097 params.param0 = upper_32_bits(req->trb_dma);
1098 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001099 cmd = DWC3_DEPCMD_STARTTRANSFER |
1100 DWC3_DEPCMD_PARAM(cmd_param);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301101 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001102 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1103 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301104 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001105
Felipe Balbi2cd47182016-04-12 16:42:43 +03001106 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001107 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001108 /*
1109 * FIXME we need to iterate over the list of requests
1110 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001111 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001112 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001113 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1114 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001115 list_del(&req->list);
1116 return ret;
1117 }
1118
1119 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001120
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001121 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001122 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001123 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001124 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001125
Felipe Balbi72246da2011-08-19 18:10:58 +03001126 return 0;
1127}
1128
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301129static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1130 struct dwc3_ep *dep, u32 cur_uf)
1131{
1132 u32 uf;
1133
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001134 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001135 dwc3_trace(trace_dwc3_gadget,
1136 "ISOC ep %s run out for requests",
1137 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301138 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301139 return;
1140 }
1141
1142 /* 4 micro frames in the future */
1143 uf = cur_uf + dep->interval * 4;
1144
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001145 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301146}
1147
1148static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1149 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1150{
1151 u32 cur_uf, mask;
1152
1153 mask = ~(dep->interval - 1);
1154 cur_uf = event->parameters & mask;
1155
1156 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1157}
1158
Felipe Balbi72246da2011-08-19 18:10:58 +03001159static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1160{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001161 struct dwc3 *dwc = dep->dwc;
1162 int ret;
1163
Felipe Balbibb423982015-11-16 15:31:21 -06001164 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001165 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001166 "trying to queue request %p to disabled %s",
Felipe Balbibb423982015-11-16 15:31:21 -06001167 &req->request, dep->endpoint.name);
1168 return -ESHUTDOWN;
1169 }
1170
1171 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1172 &req->request, req->dep->name)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001173 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001174 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001175 return -EINVAL;
1176 }
1177
Felipe Balbifc8bb912016-05-16 13:14:48 +03001178 pm_runtime_get(dwc->dev);
1179
Felipe Balbi72246da2011-08-19 18:10:58 +03001180 req->request.actual = 0;
1181 req->request.status = -EINPROGRESS;
1182 req->direction = dep->direction;
1183 req->epnum = dep->number;
1184
Felipe Balbife84f522015-09-01 09:01:38 -05001185 trace_dwc3_ep_queue(req);
1186
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001187 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1188 dep->direction);
1189 if (ret)
1190 return ret;
1191
Felipe Balbi1f512112016-08-12 13:17:27 +03001192 req->sg = req->request.sg;
1193 req->num_pending_sgs = req->request.num_mapped_sgs;
1194
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001195 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001196
Felipe Balbid889c232016-09-29 15:44:29 +03001197 /*
1198 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1199 * wait for a XferNotReady event so we will know what's the current
1200 * (micro-)frame number.
1201 *
1202 * Without this trick, we are very, very likely gonna get Bus Expiry
1203 * errors which will force us issue EndTransfer command.
1204 */
1205 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1206 if ((dep->flags & DWC3_EP_PENDING_REQUEST) &&
1207 list_empty(&dep->started_list)) {
Felipe Balbi08a36b52016-08-11 14:27:52 +03001208 dwc3_stop_active_transfer(dwc, dep->number, true);
1209 dep->flags = DWC3_EP_ENABLED;
1210 }
1211 return 0;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001212 }
1213
Felipe Balbi594e1212016-08-24 14:38:10 +03001214 if (!dwc3_calc_trbs_left(dep))
1215 return 0;
Felipe Balbib997ada2012-07-26 13:26:50 +03001216
Felipe Balbi08a36b52016-08-11 14:27:52 +03001217 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001218 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001219 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001220 "%s: failed to kick transfers",
Felipe Balbia8f32812015-09-16 10:40:07 -05001221 dep->name);
1222 if (ret == -EBUSY)
1223 ret = 0;
1224
1225 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001226}
1227
Mayank Ranaa99689a2016-08-10 17:39:47 -07001228static int dwc3_gadget_wakeup(struct usb_gadget *g)
1229{
1230 struct dwc3 *dwc = gadget_to_dwc(g);
1231
1232 schedule_work(&dwc->wakeup_work);
1233 return 0;
1234}
1235
1236static inline enum dwc3_link_state dwc3_get_link_state(struct dwc3 *dwc)
1237{
1238 u32 reg;
1239
1240 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1241 return DWC3_DSTS_USBLNKST(reg);
1242}
1243
1244static bool dwc3_gadget_is_suspended(struct dwc3 *dwc)
1245{
1246 if (atomic_read(&dwc->in_lpm) ||
1247 dwc->link_state == DWC3_LINK_STATE_U3)
1248 return true;
1249 return false;
1250}
1251
Felipe Balbi04c03d12015-12-02 10:06:45 -06001252static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1253 struct usb_request *request)
1254{
1255 dwc3_gadget_ep_free_request(ep, request);
1256}
1257
1258static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1259{
1260 struct dwc3_request *req;
1261 struct usb_request *request;
1262 struct usb_ep *ep = &dep->endpoint;
1263
Felipe Balbi60cfb372016-05-24 13:45:17 +03001264 dwc3_trace(trace_dwc3_gadget, "queueing ZLP");
Felipe Balbi04c03d12015-12-02 10:06:45 -06001265 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1266 if (!request)
1267 return -ENOMEM;
1268
1269 request->length = 0;
1270 request->buf = dwc->zlp_buf;
1271 request->complete = __dwc3_gadget_ep_zlp_complete;
1272
1273 req = to_dwc3_request(request);
1274
1275 return __dwc3_gadget_ep_queue(dep, req);
1276}
1277
Felipe Balbi72246da2011-08-19 18:10:58 +03001278static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1279 gfp_t gfp_flags)
1280{
1281 struct dwc3_request *req = to_dwc3_request(request);
1282 struct dwc3_ep *dep = to_dwc3_ep(ep);
1283 struct dwc3 *dwc = dep->dwc;
1284
1285 unsigned long flags;
1286
1287 int ret;
1288
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001289 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001290 if (!dep->endpoint.desc) {
1291 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1292 request, ep->name);
1293 ret = -ESHUTDOWN;
1294 goto out;
1295 }
1296
1297 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1298 request, req->dep->name)) {
1299 ret = -EINVAL;
1300 goto out;
1301 }
1302
1303 if (dwc3_gadget_is_suspended(dwc)) {
1304 if (dwc->gadget.remote_wakeup)
1305 dwc3_gadget_wakeup(&dwc->gadget);
1306 ret = dwc->gadget.remote_wakeup ? -EAGAIN : -ENOTSUPP;
1307 goto out;
1308 }
1309
Felipe Balbi72246da2011-08-19 18:10:58 +03001310 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001311
1312 /*
1313 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1314 * setting request->zero, instead of doing magic, we will just queue an
1315 * extra usb_request ourselves so that it gets handled the same way as
1316 * any other request.
1317 */
John Yound92618982015-12-22 12:23:20 -08001318 if (ret == 0 && request->zero && request->length &&
1319 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001320 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1321
Mayank Ranaa99689a2016-08-10 17:39:47 -07001322out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001323 spin_unlock_irqrestore(&dwc->lock, flags);
1324
1325 return ret;
1326}
1327
1328static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1329 struct usb_request *request)
1330{
1331 struct dwc3_request *req = to_dwc3_request(request);
1332 struct dwc3_request *r = NULL;
1333
1334 struct dwc3_ep *dep = to_dwc3_ep(ep);
1335 struct dwc3 *dwc = dep->dwc;
1336
1337 unsigned long flags;
1338 int ret = 0;
1339
Mayank Ranaa99689a2016-08-10 17:39:47 -07001340 if (atomic_read(&dwc->in_lpm)) {
1341 dev_err(dwc->dev, "Unable to dequeue while in LPM\n");
1342 return -EAGAIN;
1343 }
1344
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001345 trace_dwc3_ep_dequeue(req);
1346
Felipe Balbi72246da2011-08-19 18:10:58 +03001347 spin_lock_irqsave(&dwc->lock, flags);
1348
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001349 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001350 if (r == req)
1351 break;
1352 }
1353
1354 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001355 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001356 if (r == req)
1357 break;
1358 }
1359 if (r == req) {
1360 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001361 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301362 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001363 }
1364 dev_err(dwc->dev, "request %p was not queued to %s\n",
1365 request, ep->name);
1366 ret = -EINVAL;
1367 goto out0;
1368 }
1369
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301370out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001371 /* giveback the request */
1372 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1373
1374out0:
1375 spin_unlock_irqrestore(&dwc->lock, flags);
1376
1377 return ret;
1378}
1379
Felipe Balbi7a608552014-09-24 14:19:52 -05001380int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001381{
1382 struct dwc3_gadget_ep_cmd_params params;
1383 struct dwc3 *dwc = dep->dwc;
1384 int ret;
1385
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001386 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1387 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1388 return -EINVAL;
1389 }
1390
Felipe Balbi72246da2011-08-19 18:10:58 +03001391 memset(&params, 0x00, sizeof(params));
1392
1393 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001394 struct dwc3_trb *trb;
1395
1396 unsigned transfer_in_flight;
1397 unsigned started;
1398
1399 if (dep->number > 1)
1400 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1401 else
1402 trb = &dwc->ep0_trb[dep->trb_enqueue];
1403
1404 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1405 started = !list_empty(&dep->started_list);
1406
1407 if (!protocol && ((dep->direction && transfer_in_flight) ||
1408 (!dep->direction && started))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001409 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001410 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001411 dep->name);
1412 return -EAGAIN;
1413 }
1414
Felipe Balbi2cd47182016-04-12 16:42:43 +03001415 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1416 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001417 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001418 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001419 dep->name);
1420 else
1421 dep->flags |= DWC3_EP_STALL;
1422 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001423
John Youn50c763f2016-05-31 17:49:56 -07001424 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001425 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001426 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001427 dep->name);
1428 else
Alan Sterna535d812013-11-01 12:05:12 -04001429 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001430 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001431
Felipe Balbi72246da2011-08-19 18:10:58 +03001432 return ret;
1433}
1434
1435static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1436{
1437 struct dwc3_ep *dep = to_dwc3_ep(ep);
1438 struct dwc3 *dwc = dep->dwc;
1439
1440 unsigned long flags;
1441
1442 int ret;
1443
Mayank Ranaa99689a2016-08-10 17:39:47 -07001444 if (!ep->desc) {
1445 dev_err(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1446 return -EINVAL;
1447 }
1448
Felipe Balbi72246da2011-08-19 18:10:58 +03001449 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001450 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001451 spin_unlock_irqrestore(&dwc->lock, flags);
1452
1453 return ret;
1454}
1455
1456static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1457{
1458 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001459 struct dwc3 *dwc = dep->dwc;
1460 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001461 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001462
Paul Zimmerman249a4562012-02-24 17:32:16 -08001463 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001464 dep->flags |= DWC3_EP_WEDGE;
1465
Pratyush Anand08f0d962012-06-25 22:40:43 +05301466 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001467 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301468 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001469 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001470 spin_unlock_irqrestore(&dwc->lock, flags);
1471
1472 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001473}
1474
1475/* -------------------------------------------------------------------------- */
1476
1477static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1478 .bLength = USB_DT_ENDPOINT_SIZE,
1479 .bDescriptorType = USB_DT_ENDPOINT,
1480 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1481};
1482
1483static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1484 .enable = dwc3_gadget_ep0_enable,
1485 .disable = dwc3_gadget_ep0_disable,
1486 .alloc_request = dwc3_gadget_ep_alloc_request,
1487 .free_request = dwc3_gadget_ep_free_request,
1488 .queue = dwc3_gadget_ep0_queue,
1489 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301490 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001491 .set_wedge = dwc3_gadget_ep_set_wedge,
1492};
1493
1494static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1495 .enable = dwc3_gadget_ep_enable,
1496 .disable = dwc3_gadget_ep_disable,
1497 .alloc_request = dwc3_gadget_ep_alloc_request,
1498 .free_request = dwc3_gadget_ep_free_request,
1499 .queue = dwc3_gadget_ep_queue,
1500 .dequeue = dwc3_gadget_ep_dequeue,
1501 .set_halt = dwc3_gadget_ep_set_halt,
1502 .set_wedge = dwc3_gadget_ep_set_wedge,
1503};
1504
1505/* -------------------------------------------------------------------------- */
1506
1507static int dwc3_gadget_get_frame(struct usb_gadget *g)
1508{
1509 struct dwc3 *dwc = gadget_to_dwc(g);
1510 u32 reg;
1511
1512 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1513 return DWC3_DSTS_SOFFN(reg);
1514}
1515
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001516static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001517{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001518 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001519
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001520 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001521 u32 reg;
1522
Felipe Balbi72246da2011-08-19 18:10:58 +03001523 u8 link_state;
1524 u8 speed;
1525
Felipe Balbi72246da2011-08-19 18:10:58 +03001526 /*
1527 * According to the Databook Remote wakeup request should
1528 * be issued only when the device is in early suspend state.
1529 *
1530 * We can check that via USB Link State bits in DSTS register.
1531 */
1532 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1533
1534 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001535 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1536 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001537 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed");
Felipe Balbi6b742892016-05-13 10:19:42 +03001538 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001539 }
1540
1541 link_state = DWC3_DSTS_USBLNKST(reg);
1542
1543 switch (link_state) {
1544 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1545 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1546 break;
1547 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001548 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001549 "can't wakeup from '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001550 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001551 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001552 }
1553
Felipe Balbi8598bde2012-01-02 18:55:57 +02001554 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1555 if (ret < 0) {
1556 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001557 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001558 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001559
Paul Zimmerman802fde92012-04-27 13:10:52 +03001560 /* Recent versions do this automatically */
1561 if (dwc->revision < DWC3_REVISION_194A) {
1562 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001563 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001564 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1565 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1566 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001567
Paul Zimmerman1d046792012-02-15 18:56:56 -08001568 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001569 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001570
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001571 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001572 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1573
1574 /* in HS, means ON */
1575 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1576 break;
1577 }
1578
1579 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1580 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001581 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001582 }
1583
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001584 return 0;
1585}
1586
Mayank Ranaa99689a2016-08-10 17:39:47 -07001587#define DWC3_PM_RESUME_RETRIES 20 /* Max Number of retries */
1588#define DWC3_PM_RESUME_DELAY 100 /* 100 msec */
1589
1590static void dwc3_gadget_wakeup_work(struct work_struct *w)
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001591{
Mayank Ranaa99689a2016-08-10 17:39:47 -07001592 struct dwc3 *dwc;
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001593 int ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001594 static int retry_count;
1595
1596 dwc = container_of(w, struct dwc3, wakeup_work);
1597
1598 ret = pm_runtime_get_sync(dwc->dev);
1599 if (ret) {
1600 /* pm_runtime_get_sync returns -EACCES error between
1601 * late_suspend and early_resume, wait for system resume to
1602 * finish and queue work again
1603 */
1604 pr_debug("PM runtime get sync failed, ret %d\n", ret);
1605 if (ret == -EACCES) {
1606 pm_runtime_put_noidle(dwc->dev);
1607 if (retry_count == DWC3_PM_RESUME_RETRIES) {
1608 retry_count = 0;
1609 pr_err("pm_runtime_get_sync timed out\n");
1610 return;
1611 }
1612 msleep(DWC3_PM_RESUME_DELAY);
1613 retry_count++;
1614 schedule_work(&dwc->wakeup_work);
1615 return;
1616 }
1617 }
1618 retry_count = 0;
1619
1620 ret = dwc3_gadget_wakeup_int(dwc);
1621
1622 if (ret)
1623 pr_err("Remote wakeup failed. ret = %d.\n", ret);
1624 else
1625 pr_debug("Remote wakeup succeeded.\n");
1626
1627 pm_runtime_put_noidle(dwc->dev);
1628}
1629
1630static int dwc3_gadget_wakeup_int(struct dwc3 *dwc)
1631{
1632 bool link_recover_only = false;
1633
1634 u32 reg;
1635 int ret = 0;
1636 u8 link_state;
1637 unsigned long flags;
1638
1639 pr_debug("%s(): Entry\n", __func__);
1640 disable_irq(dwc->irq);
1641 spin_lock_irqsave(&dwc->lock, flags);
1642 /*
1643 * According to the Databook Remote wakeup request should
1644 * be issued only when the device is in early suspend state.
1645 *
1646 * We can check that via USB Link State bits in DSTS register.
1647 */
1648 link_state = dwc3_get_link_state(dwc);
1649
1650 switch (link_state) {
1651 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1652 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1653 break;
1654 case DWC3_LINK_STATE_U1:
1655 if (dwc->gadget.speed != USB_SPEED_SUPER) {
1656 link_recover_only = true;
1657 break;
1658 }
1659 /* Intentional fallthrough */
1660 default:
1661 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1662 link_state);
1663 ret = -EINVAL;
1664 goto out;
1665 }
1666
1667 /* Enable LINK STATUS change event */
1668 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1669 reg |= DWC3_DEVTEN_ULSTCNGEN;
1670 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1671 /*
1672 * memory barrier is required to make sure that required events
1673 * with core is enabled before performing RECOVERY mechnism.
1674 */
1675 mb();
1676
1677 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1678 if (ret < 0) {
1679 dev_err(dwc->dev, "failed to put link in Recovery\n");
1680 /* Disable LINK STATUS change */
1681 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1682 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1683 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1684 /* Required to complete this operation before returning */
1685 mb();
1686 goto out;
1687 }
1688
1689 /* Recent versions do this automatically */
1690 if (dwc->revision < DWC3_REVISION_194A) {
1691 /* write zeroes to Link Change Request */
1692 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1693 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1694 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1695 }
1696
1697 spin_unlock_irqrestore(&dwc->lock, flags);
1698 enable_irq(dwc->irq);
1699
1700 /*
1701 * Have bigger value (16 sec) for timeout since some host PCs driving
1702 * resume for very long time (e.g. 8 sec)
1703 */
1704 ret = wait_event_interruptible_timeout(dwc->wait_linkstate,
1705 (dwc->link_state < DWC3_LINK_STATE_U3) ||
1706 (dwc->link_state == DWC3_LINK_STATE_SS_DIS),
1707 msecs_to_jiffies(16000));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001708
1709 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001710 /* Disable link status change event */
1711 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1712 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1713 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1714 /*
1715 * Complete this write before we go ahead and perform resume
1716 * as we don't need link status change notificaiton anymore.
1717 */
1718 mb();
1719
1720 if (!ret) {
1721 dev_dbg(dwc->dev, "Timeout moving into state(%d)\n",
1722 dwc->link_state);
1723 ret = -EINVAL;
1724 spin_unlock_irqrestore(&dwc->lock, flags);
1725 goto out1;
1726 } else {
1727 ret = 0;
1728 /*
1729 * If USB is disconnected OR received RESET from host,
1730 * don't perform resume
1731 */
1732 if (dwc->link_state == DWC3_LINK_STATE_SS_DIS ||
1733 dwc->gadget.state == USB_STATE_DEFAULT)
1734 link_recover_only = true;
1735 }
1736
1737 /*
1738 * According to DWC3 databook, the controller does not
1739 * trigger a wakeup event when remote-wakeup is used.
1740 * Hence, after remote-wakeup sequence is complete, and
1741 * the device is back at U0 state, it is required that
1742 * the resume sequence is initiated by SW.
1743 */
1744 if (!link_recover_only)
1745 dwc3_gadget_wakeup_interrupt(dwc, true);
1746
Felipe Balbi72246da2011-08-19 18:10:58 +03001747 spin_unlock_irqrestore(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001748 pr_debug("%s: Exit\n", __func__);
1749 return ret;
1750
1751out:
1752 spin_unlock_irqrestore(&dwc->lock, flags);
1753 enable_irq(dwc->irq);
1754
1755out1:
1756 return ret;
1757}
1758
1759static int dwc_gadget_func_wakeup(struct usb_gadget *g, int interface_id)
1760{
1761 int ret = 0;
1762 struct dwc3 *dwc = gadget_to_dwc(g);
1763
1764 if (!g || (g->speed != USB_SPEED_SUPER))
1765 return -ENOTSUPP;
1766
1767 if (dwc3_gadget_is_suspended(dwc)) {
1768 pr_debug("USB bus is suspended. Scheduling wakeup and returning -EAGAIN.\n");
1769 dwc3_gadget_wakeup(&dwc->gadget);
1770 return -EAGAIN;
1771 }
1772
1773 if (dwc->revision < DWC3_REVISION_220A) {
1774 ret = dwc3_send_gadget_generic_command(dwc,
1775 DWC3_DGCMD_XMIT_FUNCTION, interface_id);
1776 } else {
1777 ret = dwc3_send_gadget_generic_command(dwc,
1778 DWC3_DGCMD_XMIT_DEV, 0x1 | (interface_id << 4));
1779 }
1780
1781 if (ret)
1782 pr_err("Function wakeup HW command failed.\n");
1783 else
1784 pr_debug("Function wakeup HW command succeeded.\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001785
1786 return ret;
1787}
1788
1789static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1790 int is_selfpowered)
1791{
1792 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001793 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001794
Paul Zimmerman249a4562012-02-24 17:32:16 -08001795 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001796 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001797 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001798
1799 return 0;
1800}
1801
Mayank Ranaa99689a2016-08-10 17:39:47 -07001802#define DWC3_SOFT_RESET_TIMEOUT 10 /* 10 msec */
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001803static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001804{
1805 u32 reg;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001806 u32 timeout = 1500;
Felipe Balbifc8bb912016-05-16 13:14:48 +03001807
Felipe Balbi72246da2011-08-19 18:10:58 +03001808 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001809 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001810 if (dwc->revision <= DWC3_REVISION_187A) {
1811 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1812 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1813 }
1814
1815 if (dwc->revision >= DWC3_REVISION_194A)
1816 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001817
1818 dwc3_event_buffers_setup(dwc);
1819 dwc3_gadget_restart(dwc);
1820 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001821 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001822
1823 if (dwc->has_hibernation)
1824 reg |= DWC3_DCTL_KEEP_CONNECT;
1825
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001826 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001827 } else {
Mayank Ranaa99689a2016-08-10 17:39:47 -07001828 dwc3_gadget_disable_irq(dwc);
1829 __dwc3_gadget_ep_disable(dwc->eps[0]);
1830 __dwc3_gadget_ep_disable(dwc->eps[1]);
1831
Felipe Balbi72246da2011-08-19 18:10:58 +03001832 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001833
1834 if (dwc->has_hibernation && !suspend)
1835 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1836
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001837 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001838 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001839
1840 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1841
1842 do {
1843 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001844 reg &= DWC3_DSTS_DEVCTRLHLT;
1845 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001846
Mayank Ranaa99689a2016-08-10 17:39:47 -07001847 if (!timeout) {
1848 dev_err(dwc->dev, "failed to %s controller\n",
1849 is_on ? "start" : "stop");
Felipe Balbif2df6792016-06-09 16:31:34 +03001850 return -ETIMEDOUT;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001851 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001852
Felipe Balbi73815282015-01-27 13:48:14 -06001853 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001854 dwc->gadget_driver
1855 ? dwc->gadget_driver->function : "no-function",
1856 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301857
1858 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001859}
1860
Mayank Ranaa99689a2016-08-10 17:39:47 -07001861static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
1862{
1863 struct dwc3 *dwc = gadget_to_dwc(g);
1864
1865 dwc->vbus_draw = mA;
1866 dev_dbg(dwc->dev, "Notify controller from %s. mA = %u\n", __func__, mA);
1867 dwc3_notify_event(dwc, DWC3_CONTROLLER_SET_CURRENT_DRAW_EVENT);
1868 return 0;
1869}
1870
Felipe Balbi72246da2011-08-19 18:10:58 +03001871static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1872{
1873 struct dwc3 *dwc = gadget_to_dwc(g);
1874 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301875 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001876
1877 is_on = !!is_on;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001878 dwc->softconnect = is_on;
1879 if ((dwc->is_drd && !dwc->vbus_active) || !dwc->gadget_driver) {
1880 /*
1881 * Need to wait for vbus_session(on) from otg driver or to
1882 * the udc_start.
1883 */
1884 return 0;
1885 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001886
Mayank Ranaa99689a2016-08-10 17:39:47 -07001887 pm_runtime_get_sync(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001888 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001889 /*
1890 * If we are here after bus suspend notify otg state machine to
1891 * increment pm usage count of dwc to prevent pm_runtime_suspend
1892 * during enumeration.
1893 */
1894 dwc->b_suspend = false;
1895 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001896 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001897 spin_unlock_irqrestore(&dwc->lock, flags);
1898
Mayank Ranaa99689a2016-08-10 17:39:47 -07001899 pm_runtime_mark_last_busy(dwc->dev);
1900 pm_runtime_put_autosuspend(dwc->dev);
1901
Pratyush Anand6f17f742012-07-02 10:21:55 +05301902 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001903}
1904
Mayank Ranaa99689a2016-08-10 17:39:47 -07001905void dwc3_gadget_enable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001906{
1907 u32 reg;
1908
1909 /* Enable all but Start and End of Frame IRQs */
1910 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1911 DWC3_DEVTEN_EVNTOVERFLOWEN |
1912 DWC3_DEVTEN_CMDCMPLTEN |
1913 DWC3_DEVTEN_ERRTICERREN |
1914 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001915 DWC3_DEVTEN_CONNECTDONEEN |
1916 DWC3_DEVTEN_USBRSTEN |
1917 DWC3_DEVTEN_DISCONNEVTEN);
1918
Mayank Ranaa99689a2016-08-10 17:39:47 -07001919 /*
1920 * Enable SUSPENDEVENT(BIT:6) for version 230A and above
1921 * else enable USB Link change event (BIT:3) for older version
1922 */
1923 if (dwc->revision < DWC3_REVISION_230A)
1924 reg |= DWC3_DEVTEN_ULSTCNGEN;
1925 else
1926 reg |= DWC3_DEVTEN_SUSPEND;
1927
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001928 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1929}
1930
Mayank Ranaa99689a2016-08-10 17:39:47 -07001931void dwc3_gadget_disable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001932{
1933 /* mask all interrupts */
1934 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1935}
1936
Felipe Balbib15a7622011-06-30 16:57:15 +03001937static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001938static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001939
Felipe Balbi4e994722016-05-13 14:09:59 +03001940/**
1941 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
1942 * dwc: pointer to our context structure
1943 *
1944 * The following looks like complex but it's actually very simple. In order to
1945 * calculate the number of packets we can burst at once on OUT transfers, we're
1946 * gonna use RxFIFO size.
1947 *
1948 * To calculate RxFIFO size we need two numbers:
1949 * MDWIDTH = size, in bits, of the internal memory bus
1950 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1951 *
1952 * Given these two numbers, the formula is simple:
1953 *
1954 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1955 *
1956 * 24 bytes is for 3x SETUP packets
1957 * 16 bytes is a clock domain crossing tolerance
1958 *
1959 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1960 */
1961static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1962{
1963 u32 ram2_depth;
1964 u32 mdwidth;
1965 u32 nump;
1966 u32 reg;
1967
1968 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1969 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1970
1971 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1972 nump = min_t(u32, nump, 16);
1973
1974 /* update NumP */
1975 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1976 reg &= ~DWC3_DCFG_NUMP_MASK;
1977 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1978 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1979}
1980
Mayank Ranaa99689a2016-08-10 17:39:47 -07001981static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1982{
1983 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1984 unsigned long flags;
1985
1986 if (!dwc->is_drd)
1987 return -EPERM;
1988
1989 is_active = !!is_active;
1990
1991 spin_lock_irqsave(&dwc->lock, flags);
1992
1993 /* Mark that the vbus was powered */
1994 dwc->vbus_active = is_active;
1995
1996 /*
1997 * Check if upper level usb_gadget_driver was already registered with
1998 * this udc controller driver (if dwc3_gadget_start was called)
1999 */
2000 if (dwc->gadget_driver && dwc->softconnect) {
2001 if (dwc->vbus_active) {
2002 /*
2003 * Both vbus was activated by otg and pullup was
2004 * signaled by the gadget driver.
2005 */
2006 dwc3_gadget_run_stop(dwc, 1, false);
2007 } else {
2008 dwc3_gadget_run_stop(dwc, 0, false);
2009 }
2010 }
2011
2012 /*
2013 * Clearing run/stop bit might occur before disconnect event is seen.
2014 * Make sure to let gadget driver know in that case.
2015 */
2016 if (!dwc->vbus_active) {
2017 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
2018 dwc3_gadget_disconnect_interrupt(dwc);
2019 }
2020
2021 spin_unlock_irqrestore(&dwc->lock, flags);
2022 return 0;
2023}
2024
Felipe Balbid7be2952016-05-04 15:49:37 +03002025static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002026{
Felipe Balbi72246da2011-08-19 18:10:58 +03002027 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002028 int ret = 0;
2029 u32 reg;
2030
Felipe Balbi72246da2011-08-19 18:10:58 +03002031 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2032 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02002033
2034 /**
2035 * WORKAROUND: DWC3 revision < 2.20a have an issue
2036 * which would cause metastability state on Run/Stop
2037 * bit if we try to force the IP to USB2-only mode.
2038 *
2039 * Because of that, we cannot configure the IP to any
2040 * speed other than the SuperSpeed
2041 *
2042 * Refers to:
2043 *
2044 * STAR#9000525659: Clock Domain Crossing on DCTL in
2045 * USB 2.0 Mode
2046 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03002047 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02002048 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002049 } else {
2050 switch (dwc->maximum_speed) {
2051 case USB_SPEED_LOW:
John Youn2da9ad72016-05-20 16:34:26 -07002052 reg |= DWC3_DCFG_LOWSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002053 break;
2054 case USB_SPEED_FULL:
John Youn2da9ad72016-05-20 16:34:26 -07002055 reg |= DWC3_DCFG_FULLSPEED1;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002056 break;
2057 case USB_SPEED_HIGH:
John Youn2da9ad72016-05-20 16:34:26 -07002058 reg |= DWC3_DCFG_HIGHSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002059 break;
John Youn75808622016-02-05 17:09:13 -08002060 case USB_SPEED_SUPER_PLUS:
John Youn2da9ad72016-05-20 16:34:26 -07002061 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
John Youn75808622016-02-05 17:09:13 -08002062 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002063 default:
John Youn77966eb2016-02-19 17:31:01 -08002064 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
Mayank Ranaa99689a2016-08-10 17:39:47 -07002065 dwc->maximum_speed);
John Youn77966eb2016-02-19 17:31:01 -08002066 /* fall through */
2067 case USB_SPEED_SUPER:
2068 reg |= DWC3_DCFG_SUPERSPEED;
2069 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002070 }
2071 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002072 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2073
Mayank Ranaa99689a2016-08-10 17:39:47 -07002074 /* Programs the number of outstanding pipelined transfer requests
2075 * the AXI master pushes to the AXI slave.
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002076 */
Mayank Ranaa99689a2016-08-10 17:39:47 -07002077 if (dwc->revision >= DWC3_REVISION_270A) {
2078 reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG1);
2079 reg &= ~DWC3_GSBUSCFG1_PIPETRANSLIMIT_MASK;
2080 reg |= DWC3_GSBUSCFG1_PIPETRANSLIMIT(0xe);
2081 dwc3_writel(dwc->regs, DWC3_GSBUSCFG1, reg);
2082 }
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002083
Felipe Balbi4e994722016-05-13 14:09:59 +03002084 dwc3_gadget_setup_nump(dwc);
2085
Felipe Balbi72246da2011-08-19 18:10:58 +03002086 /* Start with SuperSpeed Default */
2087 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2088
Mayank Ranaa99689a2016-08-10 17:39:47 -07002089 dwc->delayed_status = false;
2090 /* reinitialize physical ep0-1 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002091 dep = dwc->eps[0];
Mayank Ranaa99689a2016-08-10 17:39:47 -07002092 dep->flags = 0;
2093 dep->endpoint.maxburst = 1;
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002094 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2095 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002096 if (ret) {
2097 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002098 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002099 }
2100
2101 dep = dwc->eps[1];
Mayank Ranaa99689a2016-08-10 17:39:47 -07002102 dep->flags = 0;
2103 dep->endpoint.maxburst = 1;
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002104 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2105 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002106 if (ret) {
2107 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002108 __dwc3_gadget_ep_disable(dwc->eps[0]);
2109 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002110 }
2111
2112 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002113 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03002114 dwc3_ep0_out_start(dwc);
2115
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002116 dwc3_gadget_enable_irq(dwc);
2117
Felipe Balbid7be2952016-05-04 15:49:37 +03002118 return ret;
2119}
2120
Mayank Ranaa99689a2016-08-10 17:39:47 -07002121/* Required gadget re-initialization before switching to gadget in OTG mode */
2122void dwc3_gadget_restart(struct dwc3 *dwc)
2123{
2124 __dwc3_gadget_start(dwc);
2125}
2126
Felipe Balbid7be2952016-05-04 15:49:37 +03002127static int dwc3_gadget_start(struct usb_gadget *g,
2128 struct usb_gadget_driver *driver)
2129{
2130 struct dwc3 *dwc = gadget_to_dwc(g);
2131 unsigned long flags;
2132 int ret = 0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002133
2134 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002135
Felipe Balbid7be2952016-05-04 15:49:37 +03002136 if (dwc->gadget_driver) {
2137 dev_err(dwc->dev, "%s is already bound to %s\n",
2138 dwc->gadget.name,
2139 dwc->gadget_driver->driver.name);
2140 ret = -EBUSY;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002141 goto err0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002142 }
2143
2144 dwc->gadget_driver = driver;
2145
Mayank Ranaa99689a2016-08-10 17:39:47 -07002146 /*
2147 * For DRD, this might get called by gadget driver during bootup
2148 * even though host mode might be active. Don't actually perform
2149 * device-specific initialization until device mode is activated.
2150 * In that case dwc3_gadget_restart() will handle it.
2151 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002152 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002153 return 0;
2154
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002155err0:
Mayank Ranaa99689a2016-08-10 17:39:47 -07002156 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002157 return ret;
2158}
2159
Felipe Balbid7be2952016-05-04 15:49:37 +03002160static void __dwc3_gadget_stop(struct dwc3 *dwc)
2161{
2162 dwc3_gadget_disable_irq(dwc);
2163 __dwc3_gadget_ep_disable(dwc->eps[0]);
2164 __dwc3_gadget_ep_disable(dwc->eps[1]);
2165}
2166
Felipe Balbi22835b82014-10-17 12:05:12 -05002167static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002168{
Mayank Ranaa99689a2016-08-10 17:39:47 -07002169 struct dwc3 *dwc = gadget_to_dwc(g);
2170 unsigned long flags;
2171
Felipe Balbi72246da2011-08-19 18:10:58 +03002172 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002173 dwc->gadget_driver = NULL;
Mayank Ranabb7c0d52016-11-10 10:15:44 -08002174 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002175
2176 return 0;
2177}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002178
Mayank Ranaa99689a2016-08-10 17:39:47 -07002179static int dwc3_gadget_restart_usb_session(struct usb_gadget *g)
2180{
2181 struct dwc3 *dwc = gadget_to_dwc(g);
2182
2183 return dwc3_notify_event(dwc, DWC3_CONTROLLER_RESTART_USB_SESSION);
2184}
2185
Felipe Balbi72246da2011-08-19 18:10:58 +03002186static const struct usb_gadget_ops dwc3_gadget_ops = {
2187 .get_frame = dwc3_gadget_get_frame,
2188 .wakeup = dwc3_gadget_wakeup,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002189 .func_wakeup = dwc_gadget_func_wakeup,
Felipe Balbi72246da2011-08-19 18:10:58 +03002190 .set_selfpowered = dwc3_gadget_set_selfpowered,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002191 .vbus_session = dwc3_gadget_vbus_session,
2192 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03002193 .pullup = dwc3_gadget_pullup,
2194 .udc_start = dwc3_gadget_start,
2195 .udc_stop = dwc3_gadget_stop,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002196 .restart = dwc3_gadget_restart_usb_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03002197};
2198
2199/* -------------------------------------------------------------------------- */
2200
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002201static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
2202 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03002203{
2204 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002205 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03002206
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002207 for (i = 0; i < num; i++) {
John Yound07fa662016-05-23 11:32:43 -07002208 u8 epnum = (i << 1) | (direction ? 1 : 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002209
Felipe Balbi72246da2011-08-19 18:10:58 +03002210 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09002211 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03002212 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03002213
2214 dep->dwc = dwc;
2215 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03002216 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03002217 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03002218 dwc->eps[epnum] = dep;
2219
2220 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
2221 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002222
Felipe Balbi72246da2011-08-19 18:10:58 +03002223 dep->endpoint.name = dep->name;
Felipe Balbi74674cb2016-04-13 16:44:39 +03002224 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03002225
Felipe Balbi73815282015-01-27 13:48:14 -06002226 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03002227
Felipe Balbi72246da2011-08-19 18:10:58 +03002228 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01002229 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05302230 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002231 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2232 if (!epnum)
2233 dwc->gadget.ep0 = &dep->endpoint;
2234 } else {
2235 int ret;
2236
Robert Baldygae117e742013-12-13 12:23:38 +01002237 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01002238 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03002239 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2240 list_add_tail(&dep->endpoint.ep_list,
2241 &dwc->gadget.ep_list);
2242
2243 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002244 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002245 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002246 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002247
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002248 if (epnum == 0 || epnum == 1) {
2249 dep->endpoint.caps.type_control = true;
2250 } else {
2251 dep->endpoint.caps.type_iso = true;
2252 dep->endpoint.caps.type_bulk = true;
2253 dep->endpoint.caps.type_int = true;
2254 }
2255
2256 dep->endpoint.caps.dir_in = !!direction;
2257 dep->endpoint.caps.dir_out = !direction;
2258
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002259 INIT_LIST_HEAD(&dep->pending_list);
2260 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03002261 }
2262
2263 return 0;
2264}
2265
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002266static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
2267{
2268 int ret;
2269
2270 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2271
2272 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
2273 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06002274 dwc3_trace(trace_dwc3_gadget,
2275 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002276 return ret;
2277 }
2278
2279 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
2280 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06002281 dwc3_trace(trace_dwc3_gadget,
2282 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002283 return ret;
2284 }
2285
2286 return 0;
2287}
2288
Felipe Balbi72246da2011-08-19 18:10:58 +03002289static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2290{
2291 struct dwc3_ep *dep;
2292 u8 epnum;
2293
2294 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2295 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002296 if (!dep)
2297 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302298 /*
2299 * Physical endpoints 0 and 1 are special; they form the
2300 * bi-directional USB endpoint 0.
2301 *
2302 * For those two physical endpoints, we don't allocate a TRB
2303 * pool nor do we add them the endpoints list. Due to that, we
2304 * shouldn't do these two operations otherwise we would end up
2305 * with all sorts of bugs when removing dwc3.ko.
2306 */
2307 if (epnum != 0 && epnum != 1) {
2308 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002309 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302310 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002311
2312 kfree(dep);
2313 }
2314}
2315
Felipe Balbi72246da2011-08-19 18:10:58 +03002316/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002317
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302318static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2319 struct dwc3_request *req, struct dwc3_trb *trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002320 const struct dwc3_event_depevt *event, int status,
2321 int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302322{
2323 unsigned int count;
2324 unsigned int s_pkt = 0;
2325 unsigned int trb_status;
2326
Felipe Balbidc55c672016-08-12 13:20:32 +03002327 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002328
2329 if (req->trb == trb)
2330 dep->queued_requests--;
2331
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002332 trace_dwc3_complete_trb(dep, trb);
2333
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002334 /*
2335 * If we're in the middle of series of chained TRBs and we
2336 * receive a short transfer along the way, DWC3 will skip
2337 * through all TRBs including the last TRB in the chain (the
2338 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2339 * bit and SW has to do it manually.
2340 *
2341 * We're going to do that here to avoid problems of HW trying
2342 * to use bogus TRBs for transfers.
2343 */
2344 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2345 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2346
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302347 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Felipe Balbia0ad85a2016-08-10 18:07:46 +03002348 return 1;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002349
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302350 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbidc55c672016-08-12 13:20:32 +03002351 req->request.actual += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302352
2353 if (dep->direction) {
2354 if (count) {
2355 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2356 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002357 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002358 "%s: incomplete IN transfer",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302359 dep->name);
2360 /*
2361 * If missed isoc occurred and there is
2362 * no request queued then issue END
2363 * TRANSFER, so that core generates
2364 * next xfernotready and we will issue
2365 * a fresh START TRANSFER.
2366 * If there are still queued request
2367 * then wait, do not issue either END
2368 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002369 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302370 * giveback.If any future queued request
2371 * is successfully transferred then we
2372 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002373 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302374 */
2375 dep->flags |= DWC3_EP_MISSED_ISOC;
2376 } else {
2377 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2378 dep->name);
2379 status = -ECONNRESET;
2380 }
2381 } else {
2382 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2383 }
2384 } else {
2385 if (count && (event->status & DEPEVT_STATUS_SHORT))
2386 s_pkt = 1;
2387 }
2388
Felipe Balbi7c705df2016-08-10 12:35:30 +03002389 if (s_pkt && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302390 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002391
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302392 if ((event->status & DEPEVT_STATUS_IOC) &&
2393 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2394 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002395
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302396 return 0;
2397}
2398
Felipe Balbi72246da2011-08-19 18:10:58 +03002399static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2400 const struct dwc3_event_depevt *event, int status)
2401{
Felipe Balbi31162af2016-08-11 14:38:37 +03002402 struct dwc3_request *req, *n;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002403 struct dwc3_trb *trb;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002404 bool ioc = false;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302405 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002406
Felipe Balbi31162af2016-08-11 14:38:37 +03002407 list_for_each_entry_safe(req, n, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002408 unsigned length;
2409 unsigned actual;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002410 int chain;
2411
Felipe Balbi1f512112016-08-12 13:17:27 +03002412 length = req->request.length;
2413 chain = req->num_pending_sgs > 0;
Felipe Balbi31162af2016-08-11 14:38:37 +03002414 if (chain) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002415 struct scatterlist *sg = req->sg;
Felipe Balbi31162af2016-08-11 14:38:37 +03002416 struct scatterlist *s;
Felipe Balbi1f512112016-08-12 13:17:27 +03002417 unsigned int pending = req->num_pending_sgs;
Felipe Balbi31162af2016-08-11 14:38:37 +03002418 unsigned int i;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002419
Felipe Balbi1f512112016-08-12 13:17:27 +03002420 for_each_sg(sg, s, pending, i) {
Felipe Balbi31162af2016-08-11 14:38:37 +03002421 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbic7de5732016-07-29 03:17:58 +03002422
Felipe Balbi1f512112016-08-12 13:17:27 +03002423 req->sg = sg_next(s);
2424 req->num_pending_sgs--;
2425
Felipe Balbi31162af2016-08-11 14:38:37 +03002426 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2427 event, status, chain);
Felipe Balbi1f512112016-08-12 13:17:27 +03002428 if (ret)
2429 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002430 }
2431 } else {
Felipe Balbi737f1ae2016-08-11 12:24:27 +03002432 trb = &dep->trb_pool[dep->trb_dequeue];
Ville Syrjäläd115d702015-08-31 19:48:28 +03002433 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002434 event, status, chain);
Felipe Balbi31162af2016-08-11 14:38:37 +03002435 }
Ville Syrjäläd115d702015-08-31 19:48:28 +03002436
Felipe Balbic7de5732016-07-29 03:17:58 +03002437 /*
2438 * We assume here we will always receive the entire data block
2439 * which we should receive. Meaning, if we program RX to
2440 * receive 4K but we receive only 2K, we assume that's all we
2441 * should receive and we simply bounce the request back to the
2442 * gadget driver for further processing.
2443 */
Felipe Balbi1f512112016-08-12 13:17:27 +03002444 actual = length - req->request.actual;
2445 req->request.actual = actual;
2446
2447 if (ret && chain && (actual < length) && req->num_pending_sgs)
2448 return __dwc3_gadget_kick_transfer(dep, 0);
2449
Ville Syrjäläd115d702015-08-31 19:48:28 +03002450 dwc3_gadget_giveback(dep, req, status);
2451
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002452 if (ret) {
2453 if ((event->status & DEPEVT_STATUS_IOC) &&
2454 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2455 ioc = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002456 break;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002457 }
Felipe Balbi31162af2016-08-11 14:38:37 +03002458 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002459
Felipe Balbi4cb42212016-05-18 12:37:21 +03002460 /*
2461 * Our endpoint might get disabled by another thread during
2462 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2463 * early on so DWC3_EP_BUSY flag gets cleared
2464 */
2465 if (!dep->endpoint.desc)
2466 return 1;
2467
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302468 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002469 list_empty(&dep->started_list)) {
2470 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302471 /*
2472 * If there is no entry in request list then do
2473 * not issue END TRANSFER now. Just set PENDING
2474 * flag, so that END TRANSFER is issued when an
2475 * entry is added into request list.
2476 */
2477 dep->flags = DWC3_EP_PENDING_REQUEST;
2478 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002479 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302480 dep->flags = DWC3_EP_ENABLED;
2481 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302482 return 1;
2483 }
2484
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002485 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2486 return 0;
2487
Felipe Balbi72246da2011-08-19 18:10:58 +03002488 return 1;
2489}
2490
2491static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002492 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002493{
2494 unsigned status = 0;
2495 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002496 u32 is_xfer_complete;
2497
2498 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002499
2500 if (event->status & DEPEVT_STATUS_BUSERR)
2501 status = -ECONNRESET;
2502
Paul Zimmerman1d046792012-02-15 18:56:56 -08002503 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002504 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002505 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002506 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002507
2508 /*
2509 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2510 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2511 */
2512 if (dwc->revision < DWC3_REVISION_183A) {
2513 u32 reg;
2514 int i;
2515
2516 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002517 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002518
2519 if (!(dep->flags & DWC3_EP_ENABLED))
2520 continue;
2521
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002522 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002523 return;
2524 }
2525
2526 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2527 reg |= dwc->u1u2;
2528 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2529
2530 dwc->u1u2 = 0;
2531 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002532
Felipe Balbi4cb42212016-05-18 12:37:21 +03002533 /*
2534 * Our endpoint might get disabled by another thread during
2535 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2536 * early on so DWC3_EP_BUSY flag gets cleared
2537 */
2538 if (!dep->endpoint.desc)
2539 return;
2540
Felipe Balbie6e709b2015-09-28 15:16:56 -05002541 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002542 int ret;
2543
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002544 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002545 if (!ret || ret == -EBUSY)
2546 return;
2547 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002548}
2549
Felipe Balbi72246da2011-08-19 18:10:58 +03002550static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2551 const struct dwc3_event_depevt *event)
2552{
2553 struct dwc3_ep *dep;
2554 u8 epnum = event->endpoint_number;
2555
2556 dep = dwc->eps[epnum];
2557
Felipe Balbi3336abb2012-06-06 09:19:35 +03002558 if (!(dep->flags & DWC3_EP_ENABLED))
2559 return;
2560
Felipe Balbi72246da2011-08-19 18:10:58 +03002561 if (epnum == 0 || epnum == 1) {
2562 dwc3_ep0_interrupt(dwc, event);
2563 return;
2564 }
2565
2566 switch (event->endpoint_event) {
2567 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002568 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002569
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002570 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002571 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002572 "%s is an Isochronous endpoint",
Felipe Balbi72246da2011-08-19 18:10:58 +03002573 dep->name);
2574 return;
2575 }
2576
Jingoo Han029d97f2014-07-04 15:00:51 +09002577 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002578 break;
2579 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002580 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002581 break;
2582 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002583 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002584 dwc3_gadget_start_isoc(dwc, dep, event);
2585 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002586 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002587 int ret;
2588
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002589 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2590
Felipe Balbi73815282015-01-27 13:48:14 -06002591 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002592 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002593 : "Transfer Not Active");
2594
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002595 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002596 if (!ret || ret == -EBUSY)
2597 return;
2598
Felipe Balbiec5e7952015-11-16 16:04:13 -06002599 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002600 "%s: failed to kick transfers",
Felipe Balbi72246da2011-08-19 18:10:58 +03002601 dep->name);
2602 }
2603
2604 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002605 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002606 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002607 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2608 dep->name);
2609 return;
2610 }
2611
2612 switch (event->status) {
2613 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002614 dwc3_trace(trace_dwc3_gadget,
2615 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002616 event->parameters);
2617
2618 break;
2619 case DEPEVT_STREAMEVT_NOTFOUND:
2620 /* FALLTHROUGH */
2621 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002622 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002623 "unable to find suitable stream");
Felipe Balbi879631a2011-09-30 10:58:47 +03002624 }
2625 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002626 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi60cfb372016-05-24 13:45:17 +03002627 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002628 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002629 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002630 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002631 break;
2632 }
2633}
2634
2635static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2636{
2637 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2638 spin_unlock(&dwc->lock);
2639 dwc->gadget_driver->disconnect(&dwc->gadget);
2640 spin_lock(&dwc->lock);
2641 }
2642}
2643
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002644static void dwc3_suspend_gadget(struct dwc3 *dwc)
2645{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002646 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002647 spin_unlock(&dwc->lock);
2648 dwc->gadget_driver->suspend(&dwc->gadget);
2649 spin_lock(&dwc->lock);
2650 }
2651}
2652
2653static void dwc3_resume_gadget(struct dwc3 *dwc)
2654{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002655 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002656 spin_unlock(&dwc->lock);
2657 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002658 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002659 }
2660}
2661
2662static void dwc3_reset_gadget(struct dwc3 *dwc)
2663{
2664 if (!dwc->gadget_driver)
2665 return;
2666
2667 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2668 spin_unlock(&dwc->lock);
2669 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002670 spin_lock(&dwc->lock);
2671 }
2672}
2673
Mayank Ranaa99689a2016-08-10 17:39:47 -07002674void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002675{
2676 struct dwc3_ep *dep;
2677 struct dwc3_gadget_ep_cmd_params params;
2678 u32 cmd;
2679 int ret;
2680
2681 dep = dwc->eps[epnum];
2682
Felipe Balbib4996a82012-06-06 12:04:13 +03002683 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302684 return;
2685
Pratyush Anand57911502012-07-06 15:19:10 +05302686 /*
2687 * NOTICE: We are violating what the Databook says about the
2688 * EndTransfer command. Ideally we would _always_ wait for the
2689 * EndTransfer Command Completion IRQ, but that's causing too
2690 * much trouble synchronizing between us and gadget driver.
2691 *
2692 * We have discussed this with the IP Provider and it was
2693 * suggested to giveback all requests here, but give HW some
2694 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002695 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302696 *
2697 * Note also that a similar handling was tested by Synopsys
2698 * (thanks a lot Paul) and nothing bad has come out of it.
2699 * In short, what we're doing is:
2700 *
2701 * - Issue EndTransfer WITH CMDIOC bit set
2702 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002703 *
2704 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2705 * supports a mode to work around the above limitation. The
2706 * software can poll the CMDACT bit in the DEPCMD register
2707 * after issuing a EndTransfer command. This mode is enabled
2708 * by writing GUCTL2[14]. This polling is already done in the
2709 * dwc3_send_gadget_ep_cmd() function so if the mode is
2710 * enabled, the EndTransfer command will have completed upon
2711 * returning from this function and we don't need to delay for
2712 * 100us.
2713 *
2714 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302715 */
2716
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302717 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002718 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2719 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002720 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302721 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002722 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302723 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002724 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002725 dep->flags &= ~DWC3_EP_BUSY;
John Youn06281d42016-08-22 15:39:13 -07002726
2727 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A)
2728 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002729}
2730
2731static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2732{
2733 u32 epnum;
2734
2735 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2736 struct dwc3_ep *dep;
2737
2738 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002739 if (!dep)
2740 continue;
2741
Felipe Balbi72246da2011-08-19 18:10:58 +03002742 if (!(dep->flags & DWC3_EP_ENABLED))
2743 continue;
2744
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002745 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002746 }
2747}
2748
2749static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2750{
2751 u32 epnum;
2752
2753 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2754 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002755 int ret;
2756
2757 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002758 if (!dep)
2759 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002760
2761 if (!(dep->flags & DWC3_EP_STALL))
2762 continue;
2763
2764 dep->flags &= ~DWC3_EP_STALL;
2765
John Youn50c763f2016-05-31 17:49:56 -07002766 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002767 WARN_ON_ONCE(ret);
2768 }
2769}
2770
2771static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2772{
Felipe Balbic4430a22012-05-24 10:30:01 +03002773 int reg;
2774
Mayank Ranaa99689a2016-08-10 17:39:47 -07002775 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
2776 dwc->b_suspend = false;
2777 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
2778
Felipe Balbi72246da2011-08-19 18:10:58 +03002779 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2780 reg &= ~DWC3_DCTL_INITU1ENA;
2781 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2782
2783 reg &= ~DWC3_DCTL_INITU2ENA;
2784 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002785
Felipe Balbi72246da2011-08-19 18:10:58 +03002786 dwc3_disconnect_gadget(dwc);
2787
2788 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002789 dwc->setup_packet_pending = false;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002790 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002791 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002792
2793 dwc->connected = false;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002794 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03002795}
2796
Felipe Balbi72246da2011-08-19 18:10:58 +03002797static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2798{
2799 u32 reg;
2800
Felipe Balbifc8bb912016-05-16 13:14:48 +03002801 dwc->connected = true;
2802
Felipe Balbidf62df52011-10-14 15:11:49 +03002803 /*
2804 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2805 * would cause a missing Disconnect Event if there's a
2806 * pending Setup Packet in the FIFO.
2807 *
2808 * There's no suggested workaround on the official Bug
2809 * report, which states that "unless the driver/application
2810 * is doing any special handling of a disconnect event,
2811 * there is no functional issue".
2812 *
2813 * Unfortunately, it turns out that we _do_ some special
2814 * handling of a disconnect event, namely complete all
2815 * pending transfers, notify gadget driver of the
2816 * disconnection, and so on.
2817 *
2818 * Our suggested workaround is to follow the Disconnect
2819 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002820 * flag. Such flag gets set whenever we have a SETUP_PENDING
2821 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002822 * same endpoint.
2823 *
2824 * Refers to:
2825 *
2826 * STAR#9000466709: RTL: Device : Disconnect event not
2827 * generated if setup packet pending in FIFO
2828 */
2829 if (dwc->revision < DWC3_REVISION_188A) {
2830 if (dwc->setup_packet_pending)
2831 dwc3_gadget_disconnect_interrupt(dwc);
2832 }
2833
Mayank Ranaa99689a2016-08-10 17:39:47 -07002834 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
2835 dwc->b_suspend = false;
2836 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
2837
2838 dwc3_usb3_phy_suspend(dwc, false);
Hemant Kumard55fe952016-10-31 10:26:41 -07002839 usb_gadget_vbus_draw(&dwc->gadget, 100);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002840
Felipe Balbi8e744752014-11-06 14:27:53 +08002841 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002842
2843 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2844 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2845 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002846 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002847
2848 dwc3_stop_active_transfers(dwc);
2849 dwc3_clear_stall_all_ep(dwc);
2850
2851 /* Reset device address to zero */
2852 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2853 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2854 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002855
2856 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2857 dwc->link_state = DWC3_LINK_STATE_U0;
2858 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03002859}
2860
2861static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2862{
2863 u32 reg;
2864 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2865
2866 /*
2867 * We change the clock only at SS but I dunno why I would want to do
2868 * this. Maybe it becomes part of the power saving plan.
2869 */
2870
John Younee5cd412016-02-05 17:08:45 -08002871 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2872 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002873 return;
2874
2875 /*
2876 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2877 * each time on Connect Done.
2878 */
2879 if (!usb30_clock)
2880 return;
2881
2882 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2883 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2884 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2885}
2886
Felipe Balbi72246da2011-08-19 18:10:58 +03002887static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2888{
Felipe Balbi72246da2011-08-19 18:10:58 +03002889 struct dwc3_ep *dep;
2890 int ret;
2891 u32 reg;
2892 u8 speed;
2893
Felipe Balbi72246da2011-08-19 18:10:58 +03002894 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2895 speed = reg & DWC3_DSTS_CONNECTSPD;
2896 dwc->speed = speed;
2897
2898 dwc3_update_ram_clk_sel(dwc, speed);
2899
2900 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002901 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002902 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2903 dwc->gadget.ep0->maxpacket = 512;
2904 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2905 break;
John Youn2da9ad72016-05-20 16:34:26 -07002906 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002907 /*
2908 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2909 * would cause a missing USB3 Reset event.
2910 *
2911 * In such situations, we should force a USB3 Reset
2912 * event by calling our dwc3_gadget_reset_interrupt()
2913 * routine.
2914 *
2915 * Refers to:
2916 *
2917 * STAR#9000483510: RTL: SS : USB3 reset event may
2918 * not be generated always when the link enters poll
2919 */
2920 if (dwc->revision < DWC3_REVISION_190A)
2921 dwc3_gadget_reset_interrupt(dwc);
2922
Felipe Balbi72246da2011-08-19 18:10:58 +03002923 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2924 dwc->gadget.ep0->maxpacket = 512;
2925 dwc->gadget.speed = USB_SPEED_SUPER;
2926 break;
John Youn2da9ad72016-05-20 16:34:26 -07002927 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002928 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2929 dwc->gadget.ep0->maxpacket = 64;
2930 dwc->gadget.speed = USB_SPEED_HIGH;
2931 break;
John Youn2da9ad72016-05-20 16:34:26 -07002932 case DWC3_DSTS_FULLSPEED2:
2933 case DWC3_DSTS_FULLSPEED1:
Felipe Balbi72246da2011-08-19 18:10:58 +03002934 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2935 dwc->gadget.ep0->maxpacket = 64;
2936 dwc->gadget.speed = USB_SPEED_FULL;
2937 break;
John Youn2da9ad72016-05-20 16:34:26 -07002938 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002939 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2940 dwc->gadget.ep0->maxpacket = 8;
2941 dwc->gadget.speed = USB_SPEED_LOW;
2942 break;
2943 }
2944
Pratyush Anand2b758352013-01-14 15:59:31 +05302945 /* Enable USB2 LPM Capability */
2946
John Younee5cd412016-02-05 17:08:45 -08002947 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002948 (speed != DWC3_DSTS_SUPERSPEED) &&
2949 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302950 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2951 reg |= DWC3_DCFG_LPM_CAP;
2952 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2953
2954 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2955 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2956
Huang Rui460d0982014-10-31 11:11:18 +08002957 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302958
Huang Rui80caf7d2014-10-28 19:54:26 +08002959 /*
2960 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2961 * DCFG.LPMCap is set, core responses with an ACK and the
2962 * BESL value in the LPM token is less than or equal to LPM
2963 * NYET threshold.
2964 */
2965 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2966 && dwc->has_lpm_erratum,
2967 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2968
2969 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2970 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2971
Pratyush Anand2b758352013-01-14 15:59:31 +05302972 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002973 } else {
2974 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2975 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2976 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302977 }
2978
Mayank Ranaa99689a2016-08-10 17:39:47 -07002979
2980 /*
2981 * In HS mode this allows SS phy suspend. In SS mode this allows ss phy
2982 * suspend in P3 state and generates IN_P3 power event irq.
2983 */
2984 dwc3_usb3_phy_suspend(dwc, true);
2985
Felipe Balbi72246da2011-08-19 18:10:58 +03002986 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002987 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2988 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002989 if (ret) {
2990 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2991 return;
2992 }
2993
2994 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002995 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2996 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002997 if (ret) {
2998 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2999 return;
3000 }
3001
Mayank Ranaa99689a2016-08-10 17:39:47 -07003002 dwc3_notify_event(dwc, DWC3_CONTROLLER_CONNDONE_EVENT);
Felipe Balbi72246da2011-08-19 18:10:58 +03003003 /*
3004 * Configure PHY via GUSB3PIPECTLn if required.
3005 *
3006 * Update GTXFIFOSIZn
3007 *
3008 * In both cases reset values should be sufficient.
3009 */
3010}
3011
Mayank Ranaa99689a2016-08-10 17:39:47 -07003012static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup)
Felipe Balbi72246da2011-08-19 18:10:58 +03003013{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003014 bool perform_resume = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003015
Mayank Ranaa99689a2016-08-10 17:39:47 -07003016 dev_dbg(dwc->dev, "%s\n", __func__);
3017
3018 /*
3019 * Identify if it is called from wakeup_interrupt() context for bus
3020 * resume or as part of remote wakeup. And based on that check for
3021 * U3 state. as we need to handle case of L1 resume i.e. where we
3022 * don't want to perform resume.
3023 */
3024 if (!remote_wakeup && dwc->link_state != DWC3_LINK_STATE_U3)
3025 perform_resume = false;
3026
3027 /* Only perform resume from L2 or Early Suspend states */
3028 if (perform_resume) {
3029
3030 /*
3031 * In case of remote wake up dwc3_gadget_wakeup_work()
3032 * is doing pm_runtime_get_sync().
3033 */
3034 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3035 dwc->b_suspend = false;
3036 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
3037
3038 /*
3039 * set state to U0 as function level resume is trying to queue
3040 * notification over USB interrupt endpoint which would fail
3041 * due to state is not being updated.
3042 */
3043 dwc->link_state = DWC3_LINK_STATE_U0;
3044 dwc3_resume_gadget(dwc);
3045 return;
Jiebing Liad14d4e2014-12-11 13:26:29 +08003046 }
Mayank Ranaa99689a2016-08-10 17:39:47 -07003047
3048 dwc->link_state = DWC3_LINK_STATE_U0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003049}
3050
3051static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3052 unsigned int evtinfo)
3053{
Felipe Balbifae2b902011-10-14 13:00:30 +03003054 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003055 unsigned int pwropt;
3056
3057 /*
3058 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3059 * Hibernation mode enabled which would show up when device detects
3060 * host-initiated U3 exit.
3061 *
3062 * In that case, device will generate a Link State Change Interrupt
3063 * from U3 to RESUME which is only necessary if Hibernation is
3064 * configured in.
3065 *
3066 * There are no functional changes due to such spurious event and we
3067 * just need to ignore it.
3068 *
3069 * Refers to:
3070 *
3071 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3072 * operational mode
3073 */
3074 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3075 if ((dwc->revision < DWC3_REVISION_250A) &&
3076 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3077 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3078 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06003079 dwc3_trace(trace_dwc3_gadget,
3080 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003081 return;
3082 }
3083 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003084
3085 /*
3086 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3087 * on the link partner, the USB session might do multiple entry/exit
3088 * of low power states before a transfer takes place.
3089 *
3090 * Due to this problem, we might experience lower throughput. The
3091 * suggested workaround is to disable DCTL[12:9] bits if we're
3092 * transitioning from U1/U2 to U0 and enable those bits again
3093 * after a transfer completes and there are no pending transfers
3094 * on any of the enabled endpoints.
3095 *
3096 * This is the first half of that workaround.
3097 *
3098 * Refers to:
3099 *
3100 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3101 * core send LGO_Ux entering U0
3102 */
3103 if (dwc->revision < DWC3_REVISION_183A) {
3104 if (next == DWC3_LINK_STATE_U0) {
3105 u32 u1u2;
3106 u32 reg;
3107
3108 switch (dwc->link_state) {
3109 case DWC3_LINK_STATE_U1:
3110 case DWC3_LINK_STATE_U2:
3111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3112 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3113 | DWC3_DCTL_ACCEPTU2ENA
3114 | DWC3_DCTL_INITU1ENA
3115 | DWC3_DCTL_ACCEPTU1ENA);
3116
3117 if (!dwc->u1u2)
3118 dwc->u1u2 = reg & u1u2;
3119
3120 reg &= ~u1u2;
3121
3122 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3123 break;
3124 default:
3125 /* do nothing */
3126 break;
3127 }
3128 }
3129 }
3130
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003131 switch (next) {
3132 case DWC3_LINK_STATE_U1:
3133 if (dwc->speed == USB_SPEED_SUPER)
3134 dwc3_suspend_gadget(dwc);
3135 break;
3136 case DWC3_LINK_STATE_U2:
3137 case DWC3_LINK_STATE_U3:
3138 dwc3_suspend_gadget(dwc);
3139 break;
3140 case DWC3_LINK_STATE_RESUME:
3141 dwc3_resume_gadget(dwc);
3142 break;
3143 default:
3144 /* do nothing */
3145 break;
3146 }
3147
Mayank Ranaa99689a2016-08-10 17:39:47 -07003148 dev_dbg(dwc->dev, "Going from (%d)--->(%d)\n", dwc->link_state, next);
Felipe Balbie57ebc12014-04-22 13:20:12 -05003149 dwc->link_state = next;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003150 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003151}
3152
Baolin Wang72704f82016-05-16 16:43:53 +08003153static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
Mayank Ranaa99689a2016-08-10 17:39:47 -07003154 unsigned int evtinfo)
Baolin Wang72704f82016-05-16 16:43:53 +08003155{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003156 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Baolin Wang72704f82016-05-16 16:43:53 +08003157
Mayank Ranaa99689a2016-08-10 17:39:47 -07003158 dev_dbg(dwc->dev, "%s Entry to %d\n", __func__, next);
3159
3160 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) {
3161 /*
3162 * When first connecting the cable, even before the initial
3163 * DWC3_DEVICE_EVENT_RESET or DWC3_DEVICE_EVENT_CONNECT_DONE
3164 * events, the controller sees a DWC3_DEVICE_EVENT_SUSPEND
3165 * event. In such a case, ignore.
3166 * Ignore suspend event until device side usb is not into
3167 * CONFIGURED state.
3168 */
3169 if (dwc->gadget.state != USB_STATE_CONFIGURED) {
3170 pr_err("%s(): state:%d. Ignore SUSPEND.\n",
3171 __func__, dwc->gadget.state);
3172 return;
3173 }
3174
Baolin Wang72704f82016-05-16 16:43:53 +08003175 dwc3_suspend_gadget(dwc);
3176
Mayank Ranaa99689a2016-08-10 17:39:47 -07003177 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3178 dwc->b_suspend = true;
3179 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
3180 }
3181
Baolin Wang72704f82016-05-16 16:43:53 +08003182 dwc->link_state = next;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003183 dwc3_trace(trace_dwc3_gadget, "link state %d", dwc->link_state);
Baolin Wang72704f82016-05-16 16:43:53 +08003184}
3185
Felipe Balbie1dadd32014-02-25 14:47:54 -06003186static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3187 unsigned int evtinfo)
3188{
3189 unsigned int is_ss = evtinfo & BIT(4);
3190
3191 /**
3192 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3193 * have a known issue which can cause USB CV TD.9.23 to fail
3194 * randomly.
3195 *
3196 * Because of this issue, core could generate bogus hibernation
3197 * events which SW needs to ignore.
3198 *
3199 * Refers to:
3200 *
3201 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3202 * Device Fallback from SuperSpeed
3203 */
3204 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3205 return;
3206
3207 /* enter hibernation here */
3208}
3209
Felipe Balbi72246da2011-08-19 18:10:58 +03003210static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3211 const struct dwc3_event_devt *event)
3212{
3213 switch (event->type) {
3214 case DWC3_DEVICE_EVENT_DISCONNECT:
3215 dwc3_gadget_disconnect_interrupt(dwc);
3216 break;
3217 case DWC3_DEVICE_EVENT_RESET:
3218 dwc3_gadget_reset_interrupt(dwc);
3219 break;
3220 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3221 dwc3_gadget_conndone_interrupt(dwc);
3222 break;
3223 case DWC3_DEVICE_EVENT_WAKEUP:
Mayank Ranaa99689a2016-08-10 17:39:47 -07003224 dwc3_gadget_wakeup_interrupt(dwc, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03003225 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003226 case DWC3_DEVICE_EVENT_HIBER_REQ:
3227 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3228 "unexpected hibernation event\n"))
3229 break;
3230
3231 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3232 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003233 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3234 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3235 break;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003236 case DWC3_DEVICE_EVENT_SUSPEND:
Baolin Wang72704f82016-05-16 16:43:53 +08003237 if (dwc->revision < DWC3_REVISION_230A) {
3238 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
3239 } else {
3240 dwc3_trace(trace_dwc3_gadget, "U3/L1-L2 Suspend Event");
3241
3242 /*
3243 * Ignore suspend event until the gadget enters into
3244 * USB_STATE_CONFIGURED state.
3245 */
3246 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3247 dwc3_gadget_suspend_interrupt(dwc,
3248 event->event_info);
3249 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003250 break;
3251 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06003252 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03003253 break;
3254 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06003255 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03003256 break;
3257 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06003258 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03003259 break;
3260 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06003261 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03003262 break;
3263 default:
Felipe Balbie9f2aa872015-01-27 13:49:28 -06003264 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003265 }
Mayank Ranaa99689a2016-08-10 17:39:47 -07003266
3267 dwc->err_evt_seen = (event->type == DWC3_DEVICE_EVENT_ERRATIC_ERROR);
Felipe Balbi72246da2011-08-19 18:10:58 +03003268}
3269
3270static void dwc3_process_event_entry(struct dwc3 *dwc,
3271 const union dwc3_event *event)
3272{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003273 trace_dwc3_event(event->raw);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003274 /* skip event processing in absence of vbus */
3275 if (!dwc->vbus_active) {
3276 dev_err(dwc->dev, "SKIP EVT:%x", event->raw);
3277 return;
3278 }
3279
3280 /* If run/stop is cleared don't process any more events */
3281 if (!dwc->pullups_connected) {
3282 dev_err(dwc->dev, "SKIP_EVT_PULLUP:%x", event->raw);
3283 return;
3284 }
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003285
Felipe Balbi72246da2011-08-19 18:10:58 +03003286 /* Endpoint IRQ, handle it and return early */
3287 if (event->type.is_devspec == 0) {
3288 /* depevt */
3289 return dwc3_endpoint_interrupt(dwc, &event->depevt);
3290 }
3291
3292 switch (event->type.type) {
3293 case DWC3_EVENT_TYPE_DEV:
3294 dwc3_gadget_interrupt(dwc, &event->devt);
3295 break;
3296 /* REVISIT what to do with Carkit and I2C events ? */
3297 default:
3298 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3299 }
3300}
3301
Mayank Ranaa99689a2016-08-10 17:39:47 -07003302static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc)
Felipe Balbif42f2442013-06-12 21:25:08 +03003303{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003304 struct dwc3_event_buffer *evt;
Felipe Balbif42f2442013-06-12 21:25:08 +03003305 irqreturn_t ret = IRQ_NONE;
3306 int left;
3307 u32 reg;
3308
Mayank Ranaa99689a2016-08-10 17:39:47 -07003309 evt = dwc->ev_buf;
Felipe Balbif42f2442013-06-12 21:25:08 +03003310 left = evt->count;
3311
3312 if (!(evt->flags & DWC3_EVENT_PENDING))
3313 return IRQ_NONE;
3314
3315 while (left > 0) {
3316 union dwc3_event event;
3317
3318 event.raw = *(u32 *) (evt->buf + evt->lpos);
3319
3320 dwc3_process_event_entry(dwc, &event);
3321
Mayank Ranaa99689a2016-08-10 17:39:47 -07003322 if (dwc->err_evt_seen) {
3323 /*
3324 * if erratic error, skip remaining events
3325 * while controller undergoes reset
3326 */
3327 evt->lpos = (evt->lpos + left) %
3328 DWC3_EVENT_BUFFERS_SIZE;
3329 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), left);
3330 if (dwc3_notify_event(dwc, DWC3_CONTROLLER_ERROR_EVENT))
3331 dwc->err_evt_seen = 0;
3332 break;
3333 }
3334
Felipe Balbif42f2442013-06-12 21:25:08 +03003335 /*
3336 * FIXME we wrap around correctly to the next entry as
3337 * almost all entries are 4 bytes in size. There is one
3338 * entry which has 12 bytes which is a regular entry
3339 * followed by 8 bytes data. ATM I don't know how
3340 * things are organized if we get next to the a
3341 * boundary so I worry about that once we try to handle
3342 * that.
3343 */
3344 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
3345 left -= 4;
3346
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003347 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03003348 }
3349
Mayank Ranaa99689a2016-08-10 17:39:47 -07003350 dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] += (evt->count / 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03003351 evt->count = 0;
3352 evt->flags &= ~DWC3_EVENT_PENDING;
3353 ret = IRQ_HANDLED;
3354
3355 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003356 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003357 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003358 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003359
3360 return ret;
3361}
3362
Mayank Ranaa99689a2016-08-10 17:39:47 -07003363static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
3364{
3365 struct dwc3 *dwc = _dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05003366 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003367 irqreturn_t ret = IRQ_NONE;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003368 unsigned int temp_time;
3369 ktime_t start_time;
3370
3371 start_time = ktime_get();
Felipe Balbib15a7622011-06-30 16:57:15 +03003372
Felipe Balbie5f68b42015-10-12 13:25:44 -05003373 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003374 dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] = 0;
3375
3376 ret = dwc3_process_event_buf(dwc);
3377
Felipe Balbie5f68b42015-10-12 13:25:44 -05003378 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003379
Mayank Ranaa99689a2016-08-10 17:39:47 -07003380 temp_time = ktime_to_us(ktime_sub(ktime_get(), start_time));
3381 dwc->bh_completion_time[dwc->bh_dbg_index] = temp_time;
3382 dwc->bh_dbg_index = (dwc->bh_dbg_index + 1) % 10;
3383
Felipe Balbib15a7622011-06-30 16:57:15 +03003384 return ret;
3385}
3386
Mayank Ranaa99689a2016-08-10 17:39:47 -07003387static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003388{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003389 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003390 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003391 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003392
Mayank Ranaa99689a2016-08-10 17:39:47 -07003393 evt = dwc->ev_buf;
Felipe Balbifc8bb912016-05-16 13:14:48 +03003394
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003395 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003396 count &= DWC3_GEVNTCOUNT_MASK;
3397 if (!count)
3398 return IRQ_NONE;
3399
Mayank Ranaa99689a2016-08-10 17:39:47 -07003400 if (count > evt->length) {
3401 dev_err(dwc->dev, "HUGE_EVCNT(%d)", count);
3402 evt->lpos = (evt->lpos + count) % DWC3_EVENT_BUFFERS_SIZE;
3403 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3404 return IRQ_HANDLED;
3405 }
3406
Felipe Balbib15a7622011-06-30 16:57:15 +03003407 evt->count = count;
3408 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003409
Felipe Balbie8adfc32013-06-12 21:11:14 +03003410 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003411 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003412 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003413 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003414
Felipe Balbib15a7622011-06-30 16:57:15 +03003415 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003416}
3417
Mayank Ranaa99689a2016-08-10 17:39:47 -07003418irqreturn_t dwc3_interrupt(int irq, void *_dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003419{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003420 struct dwc3 *dwc = _dwc;
3421 irqreturn_t ret = IRQ_NONE;
3422 irqreturn_t status;
3423 unsigned int temp_cnt = 0;
3424 ktime_t start_time;
Felipe Balbi72246da2011-08-19 18:10:58 +03003425
Mayank Ranaa99689a2016-08-10 17:39:47 -07003426 start_time = ktime_get();
3427 dwc->irq_cnt++;
3428
3429 /* controller reset is still pending */
3430 if (dwc->err_evt_seen)
3431 return IRQ_HANDLED;
3432
3433 status = dwc3_check_event_buf(dwc);
3434 if (status == IRQ_WAKE_THREAD)
3435 ret = status;
3436
3437 dwc->irq_start_time[dwc->irq_dbg_index] = start_time;
3438 dwc->irq_completion_time[dwc->irq_dbg_index] =
3439 ktime_us_delta(ktime_get(), start_time);
3440 dwc->irq_event_count[dwc->irq_dbg_index] = temp_cnt / 4;
3441 dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS;
3442
Hemant Kumar78c7c282016-08-09 12:28:55 -07003443 if (ret == IRQ_WAKE_THREAD)
3444 dwc3_thread_interrupt(dwc->irq, dwc);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003445
3446 return IRQ_HANDLED;
Felipe Balbi72246da2011-08-19 18:10:58 +03003447}
3448
3449/**
3450 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003451 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003452 *
3453 * Returns 0 on success otherwise negative errno.
3454 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003455int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003456{
Roger Quadros9522def2016-06-10 14:48:38 +03003457 int ret, irq;
3458 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3459
3460 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3461 if (irq == -EPROBE_DEFER)
3462 return irq;
3463
3464 if (irq <= 0) {
3465 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3466 if (irq == -EPROBE_DEFER)
3467 return irq;
3468
3469 if (irq <= 0) {
3470 irq = platform_get_irq(dwc3_pdev, 0);
3471 if (irq <= 0) {
3472 if (irq != -EPROBE_DEFER) {
3473 dev_err(dwc->dev,
3474 "missing peripheral IRQ\n");
3475 }
3476 if (!irq)
3477 irq = -EINVAL;
3478 return irq;
3479 }
3480 }
3481 }
3482
3483 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003484
Mayank Ranaa99689a2016-08-10 17:39:47 -07003485 INIT_WORK(&dwc->wakeup_work, dwc3_gadget_wakeup_work);
3486
Felipe Balbi72246da2011-08-19 18:10:58 +03003487 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3488 &dwc->ctrl_req_addr, GFP_KERNEL);
3489 if (!dwc->ctrl_req) {
3490 dev_err(dwc->dev, "failed to allocate ctrl request\n");
3491 ret = -ENOMEM;
3492 goto err0;
3493 }
3494
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05303495 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003496 &dwc->ep0_trb_addr, GFP_KERNEL);
3497 if (!dwc->ep0_trb) {
3498 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3499 ret = -ENOMEM;
3500 goto err1;
3501 }
3502
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003503 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003504 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003505 ret = -ENOMEM;
3506 goto err2;
3507 }
3508
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003509 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003510 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
3511 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003512 if (!dwc->ep0_bounce) {
3513 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
3514 ret = -ENOMEM;
3515 goto err3;
3516 }
3517
Felipe Balbi04c03d12015-12-02 10:06:45 -06003518 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
3519 if (!dwc->zlp_buf) {
3520 ret = -ENOMEM;
3521 goto err4;
3522 }
3523
Felipe Balbi72246da2011-08-19 18:10:58 +03003524 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003525 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003526 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003527 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003528 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003529
3530 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003531 * FIXME We might be setting max_speed to <SUPER, however versions
3532 * <2.20a of dwc3 have an issue with metastability (documented
3533 * elsewhere in this driver) which tells us we can't set max speed to
3534 * anything lower than SUPER.
3535 *
3536 * Because gadget.max_speed is only used by composite.c and function
3537 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3538 * to happen so we avoid sending SuperSpeed Capability descriptor
3539 * together with our BOS descriptor as that could confuse host into
3540 * thinking we can handle super speed.
3541 *
3542 * Note that, in fact, we won't even support GetBOS requests when speed
3543 * is less than super speed because we don't have means, yet, to tell
3544 * composite.c that we are USB 2.0 + LPM ECN.
3545 */
3546 if (dwc->revision < DWC3_REVISION_220A)
3547 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03003548 "Changing max_speed on rev %08x",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003549 dwc->revision);
3550
3551 dwc->gadget.max_speed = dwc->maximum_speed;
3552
3553 /*
David Cohena4b9d942013-12-09 15:55:38 -08003554 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
3555 * on ep out.
3556 */
3557 dwc->gadget.quirk_ep_out_aligned_size = true;
3558
3559 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003560 * REVISIT: Here we should clear all pending IRQs to be
3561 * sure we're starting from a well known location.
3562 */
3563
3564 ret = dwc3_gadget_init_endpoints(dwc);
3565 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06003566 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003567
Felipe Balbi72246da2011-08-19 18:10:58 +03003568 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3569 if (ret) {
3570 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06003571 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003572 }
3573
Mayank Ranaa99689a2016-08-10 17:39:47 -07003574 if (!dwc->is_drd) {
3575 pm_runtime_no_callbacks(&dwc->gadget.dev);
3576 pm_runtime_set_active(&dwc->gadget.dev);
3577 pm_runtime_enable(&dwc->gadget.dev);
3578 pm_runtime_get(&dwc->gadget.dev);
3579 }
3580
Felipe Balbi72246da2011-08-19 18:10:58 +03003581 return 0;
3582
Felipe Balbi04c03d12015-12-02 10:06:45 -06003583err5:
3584 kfree(dwc->zlp_buf);
3585
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003586err4:
David Cohene1f80462013-09-11 17:42:47 -07003587 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003588 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
3589 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003590
Felipe Balbi72246da2011-08-19 18:10:58 +03003591err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003592 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003593
3594err2:
Christophe JAILLET51fbc7c2016-10-07 22:12:39 +02003595 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003596 dwc->ep0_trb, dwc->ep0_trb_addr);
3597
3598err1:
3599 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3600 dwc->ctrl_req, dwc->ctrl_req_addr);
3601
3602err0:
3603 return ret;
3604}
3605
Felipe Balbi7415f172012-04-30 14:56:33 +03003606/* -------------------------------------------------------------------------- */
3607
Felipe Balbi72246da2011-08-19 18:10:58 +03003608void dwc3_gadget_exit(struct dwc3 *dwc)
3609{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003610 if (dwc->is_drd) {
3611 pm_runtime_put(&dwc->gadget.dev);
3612 pm_runtime_disable(&dwc->gadget.dev);
3613 }
3614
Felipe Balbi72246da2011-08-19 18:10:58 +03003615 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003616
Felipe Balbi72246da2011-08-19 18:10:58 +03003617 dwc3_gadget_free_endpoints(dwc);
3618
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003619 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
3620 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003621
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003622 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06003623 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003624
Christophe JAILLET51fbc7c2016-10-07 22:12:39 +02003625 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003626 dwc->ep0_trb, dwc->ep0_trb_addr);
3627
3628 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3629 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003630}
Felipe Balbi7415f172012-04-30 14:56:33 +03003631
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003632int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003633{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003634 int ret;
3635
Roger Quadros9772b472016-04-12 11:33:29 +03003636 if (!dwc->gadget_driver)
3637 return 0;
3638
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003639 ret = dwc3_gadget_run_stop(dwc, false, false);
3640 if (ret < 0)
3641 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03003642
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003643 dwc3_disconnect_gadget(dwc);
3644 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003645
3646 return 0;
3647}
3648
3649int dwc3_gadget_resume(struct dwc3 *dwc)
3650{
Felipe Balbi7415f172012-04-30 14:56:33 +03003651 int ret;
3652
Roger Quadros9772b472016-04-12 11:33:29 +03003653 if (!dwc->gadget_driver)
3654 return 0;
3655
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003656 ret = __dwc3_gadget_start(dwc);
3657 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003658 goto err0;
3659
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003660 ret = dwc3_gadget_run_stop(dwc, true, false);
3661 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003662 goto err1;
3663
Felipe Balbi7415f172012-04-30 14:56:33 +03003664 return 0;
3665
3666err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003667 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003668
3669err0:
3670 return ret;
3671}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003672
3673void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3674{
3675 if (dwc->pending_events) {
3676 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3677 dwc->pending_events = false;
3678 enable_irq(dwc->irq_gadget);
3679 }
3680}