blob: 9f82436b4b1ed11992b880e862425d219f8e70de [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/list.h>
28#include <linux/dma-mapping.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
33#include "core.h"
34#include "gadget.h"
35#include "io.h"
36
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020037/**
38 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
39 * @dwc: pointer to our context structure
40 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
41 *
42 * Caller should take care of locking. This function will
43 * return 0 on success or -EINVAL if wrong Test Selector
44 * is passed
45 */
46int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
47{
48 u32 reg;
49
50 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
51 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
52
53 switch (mode) {
54 case TEST_J:
55 case TEST_K:
56 case TEST_SE0_NAK:
57 case TEST_PACKET:
58 case TEST_FORCE_EN:
59 reg |= mode << 1;
60 break;
61 default:
62 return -EINVAL;
63 }
64
65 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
66
67 return 0;
68}
69
Felipe Balbi8598bde2012-01-02 18:55:57 +020070/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030071 * dwc3_gadget_get_link_state - Gets current state of USB Link
72 * @dwc: pointer to our context structure
73 *
74 * Caller should take care of locking. This function will
75 * return the link state on success (>= 0) or -ETIMEDOUT.
76 */
77int dwc3_gadget_get_link_state(struct dwc3 *dwc)
78{
79 u32 reg;
80
81 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
82
83 return DWC3_DSTS_USBLNKST(reg);
84}
85
86/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020087 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
88 * @dwc: pointer to our context structure
89 * @state: the state to put link into
90 *
91 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080092 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020093 */
94int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
95{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080096 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020097 u32 reg;
98
Paul Zimmerman802fde92012-04-27 13:10:52 +030099 /*
100 * Wait until device controller is ready. Only applies to 1.94a and
101 * later RTL.
102 */
103 if (dwc->revision >= DWC3_REVISION_194A) {
104 while (--retries) {
105 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
106 if (reg & DWC3_DSTS_DCNRD)
107 udelay(5);
108 else
109 break;
110 }
111
112 if (retries <= 0)
113 return -ETIMEDOUT;
114 }
115
Felipe Balbi8598bde2012-01-02 18:55:57 +0200116 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
117 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
118
119 /* set requested state */
120 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
121 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
122
Paul Zimmerman802fde92012-04-27 13:10:52 +0300123 /*
124 * The following code is racy when called from dwc3_gadget_wakeup,
125 * and is not needed, at least on newer versions
126 */
127 if (dwc->revision >= DWC3_REVISION_194A)
128 return 0;
129
Felipe Balbi8598bde2012-01-02 18:55:57 +0200130 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300131 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200132 while (--retries) {
133 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
134
Felipe Balbi8598bde2012-01-02 18:55:57 +0200135 if (DWC3_DSTS_USBLNKST(reg) == state)
136 return 0;
137
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800138 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200139 }
140
141 dev_vdbg(dwc->dev, "link state change request timed out\n");
142
143 return -ETIMEDOUT;
144}
145
Felipe Balbi457e84b2012-01-18 18:04:09 +0200146/**
147 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
148 * @dwc: pointer to our context structure
149 *
150 * This function will a best effort FIFO allocation in order
151 * to improve FIFO usage and throughput, while still allowing
152 * us to enable as many endpoints as possible.
153 *
154 * Keep in mind that this operation will be highly dependent
155 * on the configured size for RAM1 - which contains TxFifo -,
156 * the amount of endpoints enabled on coreConsultant tool, and
157 * the width of the Master Bus.
158 *
159 * In the ideal world, we would always be able to satisfy the
160 * following equation:
161 *
162 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
163 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
164 *
165 * Unfortunately, due to many variables that's not always the case.
166 */
167int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
168{
169 int last_fifo_depth = 0;
170 int ram1_depth;
171 int fifo_size;
172 int mdwidth;
173 int num;
174
175 if (!dwc->needs_fifo_resize)
176 return 0;
177
178 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
179 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
180
181 /* MDWIDTH is represented in bits, we need it in bytes */
182 mdwidth >>= 3;
183
184 /*
185 * FIXME For now we will only allocate 1 wMaxPacketSize space
186 * for each enabled endpoint, later patches will come to
187 * improve this algorithm so that we better use the internal
188 * FIFO space
189 */
190 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
191 struct dwc3_ep *dep = dwc->eps[num];
192 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200193 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200194 int tmp;
195
196 if (!(dep->number & 1))
197 continue;
198
199 if (!(dep->flags & DWC3_EP_ENABLED))
200 continue;
201
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200202 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
203 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200204 mult = 3;
205
206 /*
207 * REVISIT: the following assumes we will always have enough
208 * space available on the FIFO RAM for all possible use cases.
209 * Make sure that's true somehow and change FIFO allocation
210 * accordingly.
211 *
212 * If we have Bulk or Isochronous endpoints, we want
213 * them to be able to be very, very fast. So we're giving
214 * those endpoints a fifo_size which is enough for 3 full
215 * packets
216 */
217 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200218 tmp += mdwidth;
219
220 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200221
Felipe Balbi457e84b2012-01-18 18:04:09 +0200222 fifo_size |= (last_fifo_depth << 16);
223
224 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
225 dep->name, last_fifo_depth, fifo_size & 0xffff);
226
227 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
228 fifo_size);
229
230 last_fifo_depth += (fifo_size & 0xffff);
231 }
232
233 return 0;
234}
235
Felipe Balbi72246da2011-08-19 18:10:58 +0300236void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
237 int status)
238{
239 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530240 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300241
242 if (req->queued) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530243 i = 0;
244 do {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200245 dep->busy_slot++;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530246 /*
247 * Skip LINK TRB. We can't use req->trb and check for
248 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
249 * just completed (not the LINK TRB).
250 */
251 if (((dep->busy_slot & DWC3_TRB_MASK) ==
252 DWC3_TRB_NUM- 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200253 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530254 dep->busy_slot++;
255 } while(++i < req->request.num_mapped_sgs);
Pratyush Anandc9fda7d2013-01-14 15:59:38 +0530256 req->queued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300257 }
258 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200259 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300260
261 if (req->request.status == -EINPROGRESS)
262 req->request.status = status;
263
Pratyush Anand0416e492012-08-10 13:42:16 +0530264 if (dwc->ep0_bounced && dep->number == 0)
265 dwc->ep0_bounced = false;
266 else
267 usb_gadget_unmap_request(&dwc->gadget, &req->request,
268 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300269
270 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
271 req, dep->name, req->request.actual,
272 req->request.length, status);
273
274 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200275 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300276 spin_lock(&dwc->lock);
277}
278
279static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
280{
281 switch (cmd) {
282 case DWC3_DEPCMD_DEPSTARTCFG:
283 return "Start New Configuration";
284 case DWC3_DEPCMD_ENDTRANSFER:
285 return "End Transfer";
286 case DWC3_DEPCMD_UPDATETRANSFER:
287 return "Update Transfer";
288 case DWC3_DEPCMD_STARTTRANSFER:
289 return "Start Transfer";
290 case DWC3_DEPCMD_CLEARSTALL:
291 return "Clear Stall";
292 case DWC3_DEPCMD_SETSTALL:
293 return "Set Stall";
Paul Zimmerman802fde92012-04-27 13:10:52 +0300294 case DWC3_DEPCMD_GETEPSTATE:
295 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300296 case DWC3_DEPCMD_SETTRANSFRESOURCE:
297 return "Set Endpoint Transfer Resource";
298 case DWC3_DEPCMD_SETEPCONFIG:
299 return "Set Endpoint Configuration";
300 default:
301 return "UNKNOWN command";
302 }
303}
304
Felipe Balbib09bb642012-04-24 16:19:11 +0300305int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
306{
307 u32 timeout = 500;
308 u32 reg;
309
310 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
311 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
312
313 do {
314 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
315 if (!(reg & DWC3_DGCMD_CMDACT)) {
316 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
317 DWC3_DGCMD_STATUS(reg));
318 return 0;
319 }
320
321 /*
322 * We can't sleep here, because it's also called from
323 * interrupt context.
324 */
325 timeout--;
326 if (!timeout)
327 return -ETIMEDOUT;
328 udelay(1);
329 } while (1);
330}
331
Felipe Balbi72246da2011-08-19 18:10:58 +0300332int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
333 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
334{
335 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200336 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300337 u32 reg;
338
339 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
340 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300341 dwc3_gadget_ep_cmd_string(cmd), params->param0,
342 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300343
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300344 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
345 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
346 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300347
348 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
349 do {
350 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
351 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300352 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
353 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300354 return 0;
355 }
356
357 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300358 * We can't sleep here, because it is also called from
359 * interrupt context.
360 */
361 timeout--;
362 if (!timeout)
363 return -ETIMEDOUT;
364
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200365 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300366 } while (1);
367}
368
369static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200370 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300371{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300372 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300373
374 return dep->trb_pool_dma + offset;
375}
376
377static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
378{
379 struct dwc3 *dwc = dep->dwc;
380
381 if (dep->trb_pool)
382 return 0;
383
384 if (dep->number == 0 || dep->number == 1)
385 return 0;
386
387 dep->trb_pool = dma_alloc_coherent(dwc->dev,
388 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
389 &dep->trb_pool_dma, GFP_KERNEL);
390 if (!dep->trb_pool) {
391 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
392 dep->name);
393 return -ENOMEM;
394 }
395
396 return 0;
397}
398
399static void dwc3_free_trb_pool(struct dwc3_ep *dep)
400{
401 struct dwc3 *dwc = dep->dwc;
402
403 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
404 dep->trb_pool, dep->trb_pool_dma);
405
406 dep->trb_pool = NULL;
407 dep->trb_pool_dma = 0;
408}
409
410static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
411{
412 struct dwc3_gadget_ep_cmd_params params;
413 u32 cmd;
414
415 memset(&params, 0x00, sizeof(params));
416
417 if (dep->number != 1) {
418 cmd = DWC3_DEPCMD_DEPSTARTCFG;
419 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300420 if (dep->number > 1) {
421 if (dwc->start_config_issued)
422 return 0;
423 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300424 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300425 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300426
427 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
428 }
429
430 return 0;
431}
432
433static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200434 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300435 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600436 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300437{
438 struct dwc3_gadget_ep_cmd_params params;
439
440 memset(&params, 0x00, sizeof(params));
441
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300442 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900443 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
444
445 /* Burst size is only needed in SuperSpeed mode */
446 if (dwc->gadget.speed == USB_SPEED_SUPER) {
447 u32 burst = dep->endpoint.maxburst - 1;
448
449 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
450 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300451
Felipe Balbi4b345c92012-07-16 14:08:16 +0300452 if (ignore)
453 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
454
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600455 if (restore) {
456 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
457 params.param2 |= dep->saved_state;
458 }
459
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300460 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
461 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300462
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200463 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300464 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
465 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300466 dep->stream_capable = true;
467 }
468
Felipe Balbi72246da2011-08-19 18:10:58 +0300469 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300470 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300471
472 /*
473 * We are doing 1:1 mapping for endpoints, meaning
474 * Physical Endpoints 2 maps to Logical Endpoint 2 and
475 * so on. We consider the direction bit as part of the physical
476 * endpoint number. So USB endpoint 0x81 is 0x03.
477 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300478 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300479
480 /*
481 * We must use the lower 16 TX FIFOs even though
482 * HW might have more
483 */
484 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300485 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300486
487 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300488 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300489 dep->interval = 1 << (desc->bInterval - 1);
490 }
491
492 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
493 DWC3_DEPCMD_SETEPCONFIG, &params);
494}
495
496static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
497{
498 struct dwc3_gadget_ep_cmd_params params;
499
500 memset(&params, 0x00, sizeof(params));
501
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300502 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300503
504 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
505 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
506}
507
508/**
509 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
510 * @dep: endpoint to be initialized
511 * @desc: USB Endpoint Descriptor
512 *
513 * Caller should take care of locking
514 */
515static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200516 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300517 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600518 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300519{
520 struct dwc3 *dwc = dep->dwc;
521 u32 reg;
522 int ret = -ENOMEM;
523
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300524 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
525
Felipe Balbi72246da2011-08-19 18:10:58 +0300526 if (!(dep->flags & DWC3_EP_ENABLED)) {
527 ret = dwc3_gadget_start_config(dwc, dep);
528 if (ret)
529 return ret;
530 }
531
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600532 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
533 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300534 if (ret)
535 return ret;
536
537 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200538 struct dwc3_trb *trb_st_hw;
539 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300540
541 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
542 if (ret)
543 return ret;
544
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200545 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200546 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300547 dep->type = usb_endpoint_type(desc);
548 dep->flags |= DWC3_EP_ENABLED;
549
550 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
551 reg |= DWC3_DALEPENA_EP(dep->number);
552 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
553
554 if (!usb_endpoint_xfer_isoc(desc))
555 return 0;
556
557 memset(&trb_link, 0, sizeof(trb_link));
558
Paul Zimmerman1d046792012-02-15 18:56:56 -0800559 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300560 trb_st_hw = &dep->trb_pool[0];
561
Felipe Balbif6bafc62012-02-06 11:04:53 +0200562 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300563
Felipe Balbif6bafc62012-02-06 11:04:53 +0200564 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
565 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
566 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
567 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300568 }
569
570 return 0;
571}
572
Paul Zimmermanb992e682012-04-27 14:17:35 +0300573static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200574static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300575{
576 struct dwc3_request *req;
577
Felipe Balbiea53b882012-02-17 12:10:04 +0200578 if (!list_empty(&dep->req_queued)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300579 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200580
Pratyush Anand57911502012-07-06 15:19:10 +0530581 /* - giveback all requests to gadget driver */
Pratyush Anand15916332012-06-15 11:54:36 +0530582 while (!list_empty(&dep->req_queued)) {
583 req = next_request(&dep->req_queued);
584
585 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
586 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200587 }
588
Felipe Balbi72246da2011-08-19 18:10:58 +0300589 while (!list_empty(&dep->request_list)) {
590 req = next_request(&dep->request_list);
591
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200592 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300593 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300594}
595
596/**
597 * __dwc3_gadget_ep_disable - Disables a HW endpoint
598 * @dep: the endpoint to disable
599 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200600 * This function also removes requests which are currently processed ny the
601 * hardware and those which are not yet scheduled.
602 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300603 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300604static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
605{
606 struct dwc3 *dwc = dep->dwc;
607 u32 reg;
608
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200609 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300610
611 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
612 reg &= ~DWC3_DALEPENA_EP(dep->number);
613 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
614
Felipe Balbi879631a2011-09-30 10:58:47 +0300615 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200616 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200617 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300618 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300619 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300620
621 return 0;
622}
623
624/* -------------------------------------------------------------------------- */
625
626static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
627 const struct usb_endpoint_descriptor *desc)
628{
629 return -EINVAL;
630}
631
632static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
633{
634 return -EINVAL;
635}
636
637/* -------------------------------------------------------------------------- */
638
639static int dwc3_gadget_ep_enable(struct usb_ep *ep,
640 const struct usb_endpoint_descriptor *desc)
641{
642 struct dwc3_ep *dep;
643 struct dwc3 *dwc;
644 unsigned long flags;
645 int ret;
646
647 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
648 pr_debug("dwc3: invalid parameters\n");
649 return -EINVAL;
650 }
651
652 if (!desc->wMaxPacketSize) {
653 pr_debug("dwc3: missing wMaxPacketSize\n");
654 return -EINVAL;
655 }
656
657 dep = to_dwc3_ep(ep);
658 dwc = dep->dwc;
659
Felipe Balbic6f83f32012-08-15 12:28:29 +0300660 if (dep->flags & DWC3_EP_ENABLED) {
661 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
662 dep->name);
663 return 0;
664 }
665
Felipe Balbi72246da2011-08-19 18:10:58 +0300666 switch (usb_endpoint_type(desc)) {
667 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900668 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300669 break;
670 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900671 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300672 break;
673 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900674 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300675 break;
676 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900677 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300678 break;
679 default:
680 dev_err(dwc->dev, "invalid endpoint transfer type\n");
681 }
682
Felipe Balbi72246da2011-08-19 18:10:58 +0300683 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600684 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300685 spin_unlock_irqrestore(&dwc->lock, flags);
686
687 return ret;
688}
689
690static int dwc3_gadget_ep_disable(struct usb_ep *ep)
691{
692 struct dwc3_ep *dep;
693 struct dwc3 *dwc;
694 unsigned long flags;
695 int ret;
696
697 if (!ep) {
698 pr_debug("dwc3: invalid parameters\n");
699 return -EINVAL;
700 }
701
702 dep = to_dwc3_ep(ep);
703 dwc = dep->dwc;
704
705 if (!(dep->flags & DWC3_EP_ENABLED)) {
706 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
707 dep->name);
708 return 0;
709 }
710
711 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
712 dep->number >> 1,
713 (dep->number & 1) ? "in" : "out");
714
715 spin_lock_irqsave(&dwc->lock, flags);
716 ret = __dwc3_gadget_ep_disable(dep);
717 spin_unlock_irqrestore(&dwc->lock, flags);
718
719 return ret;
720}
721
722static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
723 gfp_t gfp_flags)
724{
725 struct dwc3_request *req;
726 struct dwc3_ep *dep = to_dwc3_ep(ep);
727 struct dwc3 *dwc = dep->dwc;
728
729 req = kzalloc(sizeof(*req), gfp_flags);
730 if (!req) {
731 dev_err(dwc->dev, "not enough memory\n");
732 return NULL;
733 }
734
735 req->epnum = dep->number;
736 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300737
738 return &req->request;
739}
740
741static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
742 struct usb_request *request)
743{
744 struct dwc3_request *req = to_dwc3_request(request);
745
746 kfree(req);
747}
748
Felipe Balbic71fc372011-11-22 11:37:34 +0200749/**
750 * dwc3_prepare_one_trb - setup one TRB from one request
751 * @dep: endpoint for which this request is prepared
752 * @req: dwc3_request pointer
753 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200754static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200755 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530756 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200757{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200758 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200759 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200760
Felipe Balbieeb720f2011-11-28 12:46:59 +0200761 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
762 dep->name, req, (unsigned long long) dma,
763 length, last ? " last" : "",
764 chain ? " chain" : "");
765
Felipe Balbic71fc372011-11-22 11:37:34 +0200766 /* Skip the LINK-TRB on ISOC */
Pratyush Anand915e2022013-01-14 15:59:35 +0530767 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200768 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Pratyush Anand915e2022013-01-14 15:59:35 +0530769 dep->free_slot++;
770
771 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200772
Felipe Balbieeb720f2011-11-28 12:46:59 +0200773 if (!req->trb) {
774 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200775 req->trb = trb;
776 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530777 req->start_slot = dep->free_slot & DWC3_TRB_MASK;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200778 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200779
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530780 dep->free_slot++;
781
Felipe Balbif6bafc62012-02-06 11:04:53 +0200782 trb->size = DWC3_TRB_SIZE_LENGTH(length);
783 trb->bpl = lower_32_bits(dma);
784 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200785
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200786 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200787 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200788 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200789 break;
790
791 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530792 if (!node)
793 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
794 else
795 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbic71fc372011-11-22 11:37:34 +0200796 break;
797
798 case USB_ENDPOINT_XFER_BULK:
799 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200800 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200801 break;
802 default:
803 /*
804 * This is only possible with faulty memory because we
805 * checked it already :)
806 */
807 BUG();
808 }
809
Felipe Balbif3af3652013-12-13 14:19:33 -0600810 if (!req->request.no_interrupt && !chain)
811 trb->ctrl |= DWC3_TRB_CTRL_IOC;
812
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200813 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200814 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
815 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530816 } else if (last) {
817 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200818 }
819
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530820 if (chain)
821 trb->ctrl |= DWC3_TRB_CTRL_CHN;
822
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200823 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200824 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
825
826 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200827}
828
Felipe Balbi72246da2011-08-19 18:10:58 +0300829/*
830 * dwc3_prepare_trbs - setup TRBs from requests
831 * @dep: endpoint for which requests are being prepared
832 * @starting: true if the endpoint is idle and no requests are queued.
833 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800834 * The function goes through the requests list and sets up TRBs for the
835 * transfers. The function returns once there are no more TRBs available or
836 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300837 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200838static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300839{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200840 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300841 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200842 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200843 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300844
845 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
846
847 /* the first request must not be queued */
848 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200849
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200850 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200851 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200852 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
853 if (trbs_left > max)
854 trbs_left = max;
855 }
856
Felipe Balbi72246da2011-08-19 18:10:58 +0300857 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800858 * If busy & slot are equal than it is either full or empty. If we are
859 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300860 * full and don't do anything
861 */
862 if (!trbs_left) {
863 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200864 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300865 trbs_left = DWC3_TRB_NUM;
866 /*
867 * In case we start from scratch, we queue the ISOC requests
868 * starting from slot 1. This is done because we use ring
869 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800870 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300871 * after the first request so we start at slot 1 and have
872 * 7 requests proceed before we hit the first IOC.
873 * Other transfer types don't use the ring buffer and are
874 * processed from the first TRB until the last one. Since we
875 * don't wrap around we have to start at the beginning.
876 */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200877 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300878 dep->busy_slot = 1;
879 dep->free_slot = 1;
880 } else {
881 dep->busy_slot = 0;
882 dep->free_slot = 0;
883 }
884 }
885
886 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200887 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200888 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300889
890 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200891 unsigned length;
892 dma_addr_t dma;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530893 last_one = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300894
Felipe Balbieeb720f2011-11-28 12:46:59 +0200895 if (req->request.num_mapped_sgs > 0) {
896 struct usb_request *request = &req->request;
897 struct scatterlist *sg = request->sg;
898 struct scatterlist *s;
899 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300900
Felipe Balbieeb720f2011-11-28 12:46:59 +0200901 for_each_sg(sg, s, request->num_mapped_sgs, i) {
902 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300903
Felipe Balbieeb720f2011-11-28 12:46:59 +0200904 length = sg_dma_len(s);
905 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300906
Paul Zimmerman1d046792012-02-15 18:56:56 -0800907 if (i == (request->num_mapped_sgs - 1) ||
908 sg_is_last(s)) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530909 if (list_is_last(&req->list,
910 &dep->request_list))
911 last_one = true;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200912 chain = false;
913 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300914
Felipe Balbieeb720f2011-11-28 12:46:59 +0200915 trbs_left--;
916 if (!trbs_left)
917 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300918
Felipe Balbieeb720f2011-11-28 12:46:59 +0200919 if (last_one)
920 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300921
Felipe Balbieeb720f2011-11-28 12:46:59 +0200922 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530923 last_one, chain, i);
Felipe Balbi72246da2011-08-19 18:10:58 +0300924
Felipe Balbieeb720f2011-11-28 12:46:59 +0200925 if (last_one)
926 break;
927 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300928 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200929 dma = req->request.dma;
930 length = req->request.length;
931 trbs_left--;
932
933 if (!trbs_left)
934 last_one = 1;
935
936 /* Is this the last request? */
937 if (list_is_last(&req->list, &dep->request_list))
938 last_one = 1;
939
940 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530941 last_one, false, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200942
943 if (last_one)
944 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300945 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300946 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300947}
948
949static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
950 int start_new)
951{
952 struct dwc3_gadget_ep_cmd_params params;
953 struct dwc3_request *req;
954 struct dwc3 *dwc = dep->dwc;
955 int ret;
956 u32 cmd;
957
958 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
959 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
960 return -EBUSY;
961 }
962 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
963
964 /*
965 * If we are getting here after a short-out-packet we don't enqueue any
966 * new requests as we try to set the IOC bit only on the last request.
967 */
968 if (start_new) {
969 if (list_empty(&dep->req_queued))
970 dwc3_prepare_trbs(dep, start_new);
971
972 /* req points to the first request which will be sent */
973 req = next_request(&dep->req_queued);
974 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200975 dwc3_prepare_trbs(dep, start_new);
976
Felipe Balbi72246da2011-08-19 18:10:58 +0300977 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800978 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300979 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200980 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300981 }
982 if (!req) {
983 dep->flags |= DWC3_EP_PENDING_REQUEST;
984 return 0;
985 }
986
987 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +0300988
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530989 if (start_new) {
990 params.param0 = upper_32_bits(req->trb_dma);
991 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300992 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530993 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +0300994 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530995 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300996
997 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
998 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
999 if (ret < 0) {
1000 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
1001
1002 /*
1003 * FIXME we need to iterate over the list of requests
1004 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001005 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001006 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001007 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1008 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001009 list_del(&req->list);
1010 return ret;
1011 }
1012
1013 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001014
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001015 if (start_new) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001016 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001017 dep->number);
Felipe Balbib4996a82012-06-06 12:04:13 +03001018 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001019 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001020
Felipe Balbi72246da2011-08-19 18:10:58 +03001021 return 0;
1022}
1023
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301024static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1025 struct dwc3_ep *dep, u32 cur_uf)
1026{
1027 u32 uf;
1028
1029 if (list_empty(&dep->request_list)) {
1030 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1031 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301032 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301033 return;
1034 }
1035
1036 /* 4 micro frames in the future */
1037 uf = cur_uf + dep->interval * 4;
1038
1039 __dwc3_gadget_kick_transfer(dep, uf, 1);
1040}
1041
1042static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1043 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1044{
1045 u32 cur_uf, mask;
1046
1047 mask = ~(dep->interval - 1);
1048 cur_uf = event->parameters & mask;
1049
1050 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1051}
1052
Felipe Balbi72246da2011-08-19 18:10:58 +03001053static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1054{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001055 struct dwc3 *dwc = dep->dwc;
1056 int ret;
1057
Felipe Balbi72246da2011-08-19 18:10:58 +03001058 req->request.actual = 0;
1059 req->request.status = -EINPROGRESS;
1060 req->direction = dep->direction;
1061 req->epnum = dep->number;
1062
1063 /*
1064 * We only add to our list of requests now and
1065 * start consuming the list once we get XferNotReady
1066 * IRQ.
1067 *
1068 * That way, we avoid doing anything that we don't need
1069 * to do now and defer it until the point we receive a
1070 * particular token from the Host side.
1071 *
1072 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001073 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001074 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001075 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1076 dep->direction);
1077 if (ret)
1078 return ret;
1079
Felipe Balbi72246da2011-08-19 18:10:58 +03001080 list_add_tail(&req->list, &dep->request_list);
1081
1082 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001083 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001084 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001085 * 1. XferNotReady with empty list of requests. We need to kick the
1086 * transfer here in that situation, otherwise we will be NAKing
1087 * forever. If we get XferNotReady before gadget driver has a
1088 * chance to queue a request, we will ACK the IRQ but won't be
1089 * able to receive the data until the next request is queued.
1090 * The following code is handling exactly that.
1091 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001092 */
1093 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301094 /*
1095 * If xfernotready is already elapsed and it is a case
1096 * of isoc transfer, then issue END TRANSFER, so that
1097 * you can receive xfernotready again and can have
1098 * notion of current microframe.
1099 */
1100 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301101 if (list_empty(&dep->req_queued)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001102 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301103 dep->flags = DWC3_EP_ENABLED;
1104 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301105 return 0;
1106 }
1107
Felipe Balbib511e5e2012-06-06 12:00:50 +03001108 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Moiz Sonasath348e0262012-08-01 14:08:30 -05001109 if (ret && ret != -EBUSY)
Felipe Balbi72246da2011-08-19 18:10:58 +03001110 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1111 dep->name);
Pratyush Anand15f86bd2013-01-14 15:59:33 +05301112 return ret;
Felipe Balbia0925322012-05-22 10:24:11 +03001113 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001114
Felipe Balbib511e5e2012-06-06 12:00:50 +03001115 /*
1116 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1117 * kick the transfer here after queuing a request, otherwise the
1118 * core may not see the modified TRB(s).
1119 */
1120 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301121 (dep->flags & DWC3_EP_BUSY) &&
1122 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001123 WARN_ON_ONCE(!dep->resource_index);
1124 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbib511e5e2012-06-06 12:00:50 +03001125 false);
Moiz Sonasath348e0262012-08-01 14:08:30 -05001126 if (ret && ret != -EBUSY)
Felipe Balbib511e5e2012-06-06 12:00:50 +03001127 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1128 dep->name);
Pratyush Anand15f86bd2013-01-14 15:59:33 +05301129 return ret;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001130 }
1131
Felipe Balbi72246da2011-08-19 18:10:58 +03001132 return 0;
1133}
1134
1135static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1136 gfp_t gfp_flags)
1137{
1138 struct dwc3_request *req = to_dwc3_request(request);
1139 struct dwc3_ep *dep = to_dwc3_ep(ep);
1140 struct dwc3 *dwc = dep->dwc;
1141
1142 unsigned long flags;
1143
1144 int ret;
1145
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001146 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001147 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1148 request, ep->name);
1149 return -ESHUTDOWN;
1150 }
1151
1152 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1153 request, ep->name, request->length);
1154
1155 spin_lock_irqsave(&dwc->lock, flags);
1156 ret = __dwc3_gadget_ep_queue(dep, req);
1157 spin_unlock_irqrestore(&dwc->lock, flags);
1158
1159 return ret;
1160}
1161
1162static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1163 struct usb_request *request)
1164{
1165 struct dwc3_request *req = to_dwc3_request(request);
1166 struct dwc3_request *r = NULL;
1167
1168 struct dwc3_ep *dep = to_dwc3_ep(ep);
1169 struct dwc3 *dwc = dep->dwc;
1170
1171 unsigned long flags;
1172 int ret = 0;
1173
1174 spin_lock_irqsave(&dwc->lock, flags);
1175
1176 list_for_each_entry(r, &dep->request_list, list) {
1177 if (r == req)
1178 break;
1179 }
1180
1181 if (r != req) {
1182 list_for_each_entry(r, &dep->req_queued, list) {
1183 if (r == req)
1184 break;
1185 }
1186 if (r == req) {
1187 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001188 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301189 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001190 }
1191 dev_err(dwc->dev, "request %p was not queued to %s\n",
1192 request, ep->name);
1193 ret = -EINVAL;
1194 goto out0;
1195 }
1196
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301197out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001198 /* giveback the request */
1199 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1200
1201out0:
1202 spin_unlock_irqrestore(&dwc->lock, flags);
1203
1204 return ret;
1205}
1206
1207int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1208{
1209 struct dwc3_gadget_ep_cmd_params params;
1210 struct dwc3 *dwc = dep->dwc;
1211 int ret;
1212
1213 memset(&params, 0x00, sizeof(params));
1214
1215 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001216 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1217 DWC3_DEPCMD_SETSTALL, &params);
1218 if (ret)
1219 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1220 value ? "set" : "clear",
1221 dep->name);
1222 else
1223 dep->flags |= DWC3_EP_STALL;
1224 } else {
1225 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1226 DWC3_DEPCMD_CLEARSTALL, &params);
1227 if (ret)
1228 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1229 value ? "set" : "clear",
1230 dep->name);
1231 else
Alan Sterna535d812013-11-01 12:05:12 -04001232 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001233 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001234
Felipe Balbi72246da2011-08-19 18:10:58 +03001235 return ret;
1236}
1237
1238static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1239{
1240 struct dwc3_ep *dep = to_dwc3_ep(ep);
1241 struct dwc3 *dwc = dep->dwc;
1242
1243 unsigned long flags;
1244
1245 int ret;
1246
1247 spin_lock_irqsave(&dwc->lock, flags);
1248
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001249 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001250 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1251 ret = -EINVAL;
1252 goto out;
1253 }
1254
1255 ret = __dwc3_gadget_ep_set_halt(dep, value);
1256out:
1257 spin_unlock_irqrestore(&dwc->lock, flags);
1258
1259 return ret;
1260}
1261
1262static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1263{
1264 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001265 struct dwc3 *dwc = dep->dwc;
1266 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001267
Paul Zimmerman249a4562012-02-24 17:32:16 -08001268 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001269 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001270 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001271
Pratyush Anand08f0d962012-06-25 22:40:43 +05301272 if (dep->number == 0 || dep->number == 1)
1273 return dwc3_gadget_ep0_set_halt(ep, 1);
1274 else
1275 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001276}
1277
1278/* -------------------------------------------------------------------------- */
1279
1280static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1281 .bLength = USB_DT_ENDPOINT_SIZE,
1282 .bDescriptorType = USB_DT_ENDPOINT,
1283 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1284};
1285
1286static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1287 .enable = dwc3_gadget_ep0_enable,
1288 .disable = dwc3_gadget_ep0_disable,
1289 .alloc_request = dwc3_gadget_ep_alloc_request,
1290 .free_request = dwc3_gadget_ep_free_request,
1291 .queue = dwc3_gadget_ep0_queue,
1292 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301293 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001294 .set_wedge = dwc3_gadget_ep_set_wedge,
1295};
1296
1297static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1298 .enable = dwc3_gadget_ep_enable,
1299 .disable = dwc3_gadget_ep_disable,
1300 .alloc_request = dwc3_gadget_ep_alloc_request,
1301 .free_request = dwc3_gadget_ep_free_request,
1302 .queue = dwc3_gadget_ep_queue,
1303 .dequeue = dwc3_gadget_ep_dequeue,
1304 .set_halt = dwc3_gadget_ep_set_halt,
1305 .set_wedge = dwc3_gadget_ep_set_wedge,
1306};
1307
1308/* -------------------------------------------------------------------------- */
1309
1310static int dwc3_gadget_get_frame(struct usb_gadget *g)
1311{
1312 struct dwc3 *dwc = gadget_to_dwc(g);
1313 u32 reg;
1314
1315 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1316 return DWC3_DSTS_SOFFN(reg);
1317}
1318
1319static int dwc3_gadget_wakeup(struct usb_gadget *g)
1320{
1321 struct dwc3 *dwc = gadget_to_dwc(g);
1322
1323 unsigned long timeout;
1324 unsigned long flags;
1325
1326 u32 reg;
1327
1328 int ret = 0;
1329
1330 u8 link_state;
1331 u8 speed;
1332
1333 spin_lock_irqsave(&dwc->lock, flags);
1334
1335 /*
1336 * According to the Databook Remote wakeup request should
1337 * be issued only when the device is in early suspend state.
1338 *
1339 * We can check that via USB Link State bits in DSTS register.
1340 */
1341 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1342
1343 speed = reg & DWC3_DSTS_CONNECTSPD;
1344 if (speed == DWC3_DSTS_SUPERSPEED) {
1345 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1346 ret = -EINVAL;
1347 goto out;
1348 }
1349
1350 link_state = DWC3_DSTS_USBLNKST(reg);
1351
1352 switch (link_state) {
1353 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1354 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1355 break;
1356 default:
1357 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1358 link_state);
1359 ret = -EINVAL;
1360 goto out;
1361 }
1362
Felipe Balbi8598bde2012-01-02 18:55:57 +02001363 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1364 if (ret < 0) {
1365 dev_err(dwc->dev, "failed to put link in Recovery\n");
1366 goto out;
1367 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001368
Paul Zimmerman802fde92012-04-27 13:10:52 +03001369 /* Recent versions do this automatically */
1370 if (dwc->revision < DWC3_REVISION_194A) {
1371 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001372 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001373 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1374 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1375 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001376
Paul Zimmerman1d046792012-02-15 18:56:56 -08001377 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001378 timeout = jiffies + msecs_to_jiffies(100);
1379
Paul Zimmerman1d046792012-02-15 18:56:56 -08001380 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001381 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1382
1383 /* in HS, means ON */
1384 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1385 break;
1386 }
1387
1388 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1389 dev_err(dwc->dev, "failed to send remote wakeup\n");
1390 ret = -EINVAL;
1391 }
1392
1393out:
1394 spin_unlock_irqrestore(&dwc->lock, flags);
1395
1396 return ret;
1397}
1398
1399static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1400 int is_selfpowered)
1401{
1402 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001403 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001404
Paul Zimmerman249a4562012-02-24 17:32:16 -08001405 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001406 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001407 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001408
1409 return 0;
1410}
1411
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001412static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001413{
1414 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001415 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001416
1417 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001418 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001419 if (dwc->revision <= DWC3_REVISION_187A) {
1420 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1421 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1422 }
1423
1424 if (dwc->revision >= DWC3_REVISION_194A)
1425 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1426 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001427
1428 if (dwc->has_hibernation)
1429 reg |= DWC3_DCTL_KEEP_CONNECT;
1430
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001431 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001432 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001433 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001434
1435 if (dwc->has_hibernation && !suspend)
1436 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1437
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001438 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001439 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001440
1441 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1442
1443 do {
1444 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1445 if (is_on) {
1446 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1447 break;
1448 } else {
1449 if (reg & DWC3_DSTS_DEVCTRLHLT)
1450 break;
1451 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001452 timeout--;
1453 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301454 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001455 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001456 } while (1);
1457
1458 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1459 dwc->gadget_driver
1460 ? dwc->gadget_driver->function : "no-function",
1461 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301462
1463 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001464}
1465
1466static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1467{
1468 struct dwc3 *dwc = gadget_to_dwc(g);
1469 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301470 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001471
1472 is_on = !!is_on;
1473
1474 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001475 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001476 spin_unlock_irqrestore(&dwc->lock, flags);
1477
Pratyush Anand6f17f742012-07-02 10:21:55 +05301478 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001479}
1480
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001481static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1482{
1483 u32 reg;
1484
1485 /* Enable all but Start and End of Frame IRQs */
1486 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1487 DWC3_DEVTEN_EVNTOVERFLOWEN |
1488 DWC3_DEVTEN_CMDCMPLTEN |
1489 DWC3_DEVTEN_ERRTICERREN |
1490 DWC3_DEVTEN_WKUPEVTEN |
1491 DWC3_DEVTEN_ULSTCNGEN |
1492 DWC3_DEVTEN_CONNECTDONEEN |
1493 DWC3_DEVTEN_USBRSTEN |
1494 DWC3_DEVTEN_DISCONNEVTEN);
1495
1496 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1497}
1498
1499static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1500{
1501 /* mask all interrupts */
1502 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1503}
1504
1505static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001506static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001507
Felipe Balbi72246da2011-08-19 18:10:58 +03001508static int dwc3_gadget_start(struct usb_gadget *g,
1509 struct usb_gadget_driver *driver)
1510{
1511 struct dwc3 *dwc = gadget_to_dwc(g);
1512 struct dwc3_ep *dep;
1513 unsigned long flags;
1514 int ret = 0;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001515 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001516 u32 reg;
1517
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001518 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1519 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
Felipe Balbie8adfc32013-06-12 21:11:14 +03001520 IRQF_SHARED, "dwc3", dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001521 if (ret) {
1522 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1523 irq, ret);
1524 goto err0;
1525 }
1526
Felipe Balbi72246da2011-08-19 18:10:58 +03001527 spin_lock_irqsave(&dwc->lock, flags);
1528
1529 if (dwc->gadget_driver) {
1530 dev_err(dwc->dev, "%s is already bound to %s\n",
1531 dwc->gadget.name,
1532 dwc->gadget_driver->driver.name);
1533 ret = -EBUSY;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001534 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001535 }
1536
1537 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03001538
Felipe Balbi72246da2011-08-19 18:10:58 +03001539 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1540 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001541
1542 /**
1543 * WORKAROUND: DWC3 revision < 2.20a have an issue
1544 * which would cause metastability state on Run/Stop
1545 * bit if we try to force the IP to USB2-only mode.
1546 *
1547 * Because of that, we cannot configure the IP to any
1548 * speed other than the SuperSpeed
1549 *
1550 * Refers to:
1551 *
1552 * STAR#9000525659: Clock Domain Crossing on DCTL in
1553 * USB 2.0 Mode
1554 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001555 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001556 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001557 } else {
1558 switch (dwc->maximum_speed) {
1559 case USB_SPEED_LOW:
1560 reg |= DWC3_DSTS_LOWSPEED;
1561 break;
1562 case USB_SPEED_FULL:
1563 reg |= DWC3_DSTS_FULLSPEED1;
1564 break;
1565 case USB_SPEED_HIGH:
1566 reg |= DWC3_DSTS_HIGHSPEED;
1567 break;
1568 case USB_SPEED_SUPER: /* FALLTHROUGH */
1569 case USB_SPEED_UNKNOWN: /* FALTHROUGH */
1570 default:
1571 reg |= DWC3_DSTS_SUPERSPEED;
1572 }
1573 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001574 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1575
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001576 dwc->start_config_issued = false;
1577
Felipe Balbi72246da2011-08-19 18:10:58 +03001578 /* Start with SuperSpeed Default */
1579 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1580
1581 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001582 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1583 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001584 if (ret) {
1585 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001586 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +03001587 }
1588
1589 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001590 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1591 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001592 if (ret) {
1593 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001594 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03001595 }
1596
1597 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001598 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001599 dwc3_ep0_out_start(dwc);
1600
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001601 dwc3_gadget_enable_irq(dwc);
1602
Felipe Balbi72246da2011-08-19 18:10:58 +03001603 spin_unlock_irqrestore(&dwc->lock, flags);
1604
1605 return 0;
1606
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001607err3:
Felipe Balbi72246da2011-08-19 18:10:58 +03001608 __dwc3_gadget_ep_disable(dwc->eps[0]);
1609
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001610err2:
Felipe Balbicdcedd62013-07-15 12:36:35 +03001611 dwc->gadget_driver = NULL;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001612
1613err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001614 spin_unlock_irqrestore(&dwc->lock, flags);
1615
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001616 free_irq(irq, dwc);
1617
1618err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001619 return ret;
1620}
1621
1622static int dwc3_gadget_stop(struct usb_gadget *g,
1623 struct usb_gadget_driver *driver)
1624{
1625 struct dwc3 *dwc = gadget_to_dwc(g);
1626 unsigned long flags;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001627 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001628
1629 spin_lock_irqsave(&dwc->lock, flags);
1630
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001631 dwc3_gadget_disable_irq(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001632 __dwc3_gadget_ep_disable(dwc->eps[0]);
1633 __dwc3_gadget_ep_disable(dwc->eps[1]);
1634
1635 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001636
1637 spin_unlock_irqrestore(&dwc->lock, flags);
1638
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001639 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1640 free_irq(irq, dwc);
1641
Felipe Balbi72246da2011-08-19 18:10:58 +03001642 return 0;
1643}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001644
Felipe Balbi72246da2011-08-19 18:10:58 +03001645static const struct usb_gadget_ops dwc3_gadget_ops = {
1646 .get_frame = dwc3_gadget_get_frame,
1647 .wakeup = dwc3_gadget_wakeup,
1648 .set_selfpowered = dwc3_gadget_set_selfpowered,
1649 .pullup = dwc3_gadget_pullup,
1650 .udc_start = dwc3_gadget_start,
1651 .udc_stop = dwc3_gadget_stop,
1652};
1653
1654/* -------------------------------------------------------------------------- */
1655
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001656static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1657 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001658{
1659 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001660 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001661
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001662 for (i = 0; i < num; i++) {
1663 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001664
Felipe Balbi72246da2011-08-19 18:10:58 +03001665 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1666 if (!dep) {
1667 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1668 epnum);
1669 return -ENOMEM;
1670 }
1671
1672 dep->dwc = dwc;
1673 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001674 dep->direction = !!direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001675 dwc->eps[epnum] = dep;
1676
1677 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1678 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001679
Felipe Balbi72246da2011-08-19 18:10:58 +03001680 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001681
Felipe Balbi653df352013-07-12 19:11:57 +03001682 dev_vdbg(dwc->dev, "initializing %s\n", dep->name);
1683
Felipe Balbi72246da2011-08-19 18:10:58 +03001684 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001685 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301686 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001687 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1688 if (!epnum)
1689 dwc->gadget.ep0 = &dep->endpoint;
1690 } else {
1691 int ret;
1692
Robert Baldygae117e742013-12-13 12:23:38 +01001693 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001694 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001695 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1696 list_add_tail(&dep->endpoint.ep_list,
1697 &dwc->gadget.ep_list);
1698
1699 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001700 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001701 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001702 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001703
Felipe Balbi72246da2011-08-19 18:10:58 +03001704 INIT_LIST_HEAD(&dep->request_list);
1705 INIT_LIST_HEAD(&dep->req_queued);
1706 }
1707
1708 return 0;
1709}
1710
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001711static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1712{
1713 int ret;
1714
1715 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1716
1717 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1718 if (ret < 0) {
1719 dev_vdbg(dwc->dev, "failed to allocate OUT endpoints\n");
1720 return ret;
1721 }
1722
1723 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1724 if (ret < 0) {
1725 dev_vdbg(dwc->dev, "failed to allocate IN endpoints\n");
1726 return ret;
1727 }
1728
1729 return 0;
1730}
1731
Felipe Balbi72246da2011-08-19 18:10:58 +03001732static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1733{
1734 struct dwc3_ep *dep;
1735 u8 epnum;
1736
1737 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1738 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001739 if (!dep)
1740 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301741 /*
1742 * Physical endpoints 0 and 1 are special; they form the
1743 * bi-directional USB endpoint 0.
1744 *
1745 * For those two physical endpoints, we don't allocate a TRB
1746 * pool nor do we add them the endpoints list. Due to that, we
1747 * shouldn't do these two operations otherwise we would end up
1748 * with all sorts of bugs when removing dwc3.ko.
1749 */
1750 if (epnum != 0 && epnum != 1) {
1751 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001752 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301753 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001754
1755 kfree(dep);
1756 }
1757}
1758
Felipe Balbi72246da2011-08-19 18:10:58 +03001759/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001760
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301761static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1762 struct dwc3_request *req, struct dwc3_trb *trb,
1763 const struct dwc3_event_depevt *event, int status)
1764{
1765 unsigned int count;
1766 unsigned int s_pkt = 0;
1767 unsigned int trb_status;
1768
1769 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1770 /*
1771 * We continue despite the error. There is not much we
1772 * can do. If we don't clean it up we loop forever. If
1773 * we skip the TRB then it gets overwritten after a
1774 * while since we use them in a ring buffer. A BUG()
1775 * would help. Lets hope that if this occurs, someone
1776 * fixes the root cause instead of looking away :)
1777 */
1778 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1779 dep->name, trb);
1780 count = trb->size & DWC3_TRB_SIZE_MASK;
1781
1782 if (dep->direction) {
1783 if (count) {
1784 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1785 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1786 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1787 dep->name);
1788 /*
1789 * If missed isoc occurred and there is
1790 * no request queued then issue END
1791 * TRANSFER, so that core generates
1792 * next xfernotready and we will issue
1793 * a fresh START TRANSFER.
1794 * If there are still queued request
1795 * then wait, do not issue either END
1796 * or UPDATE TRANSFER, just attach next
1797 * request in request_list during
1798 * giveback.If any future queued request
1799 * is successfully transferred then we
1800 * will issue UPDATE TRANSFER for all
1801 * request in the request_list.
1802 */
1803 dep->flags |= DWC3_EP_MISSED_ISOC;
1804 } else {
1805 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1806 dep->name);
1807 status = -ECONNRESET;
1808 }
1809 } else {
1810 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1811 }
1812 } else {
1813 if (count && (event->status & DEPEVT_STATUS_SHORT))
1814 s_pkt = 1;
1815 }
1816
1817 /*
1818 * We assume here we will always receive the entire data block
1819 * which we should receive. Meaning, if we program RX to
1820 * receive 4K but we receive only 2K, we assume that's all we
1821 * should receive and we simply bounce the request back to the
1822 * gadget driver for further processing.
1823 */
1824 req->request.actual += req->request.length - count;
1825 if (s_pkt)
1826 return 1;
1827 if ((event->status & DEPEVT_STATUS_LST) &&
1828 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1829 DWC3_TRB_CTRL_HWO)))
1830 return 1;
1831 if ((event->status & DEPEVT_STATUS_IOC) &&
1832 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1833 return 1;
1834 return 0;
1835}
1836
Felipe Balbi72246da2011-08-19 18:10:58 +03001837static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1838 const struct dwc3_event_depevt *event, int status)
1839{
1840 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001841 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301842 unsigned int slot;
1843 unsigned int i;
1844 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001845
1846 do {
1847 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001848 if (!req) {
1849 WARN_ON_ONCE(1);
1850 return 1;
1851 }
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301852 i = 0;
1853 do {
1854 slot = req->start_slot + i;
1855 if ((slot == DWC3_TRB_NUM - 1) &&
1856 usb_endpoint_xfer_isoc(dep->endpoint.desc))
1857 slot++;
1858 slot %= DWC3_TRB_NUM;
1859 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03001860
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301861 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1862 event, status);
1863 if (ret)
1864 break;
1865 }while (++i < req->request.num_mapped_sgs);
Felipe Balbi72246da2011-08-19 18:10:58 +03001866
Felipe Balbi72246da2011-08-19 18:10:58 +03001867 dwc3_gadget_giveback(dep, req, status);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301868
1869 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001870 break;
1871 } while (1);
1872
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301873 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1874 list_empty(&dep->req_queued)) {
1875 if (list_empty(&dep->request_list)) {
1876 /*
1877 * If there is no entry in request list then do
1878 * not issue END TRANSFER now. Just set PENDING
1879 * flag, so that END TRANSFER is issued when an
1880 * entry is added into request list.
1881 */
1882 dep->flags = DWC3_EP_PENDING_REQUEST;
1883 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001884 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301885 dep->flags = DWC3_EP_ENABLED;
1886 }
Pratyush Anand7efea862013-01-14 15:59:32 +05301887 return 1;
1888 }
1889
Felipe Balbi72246da2011-08-19 18:10:58 +03001890 return 1;
1891}
1892
1893static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1894 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1895 int start_new)
1896{
1897 unsigned status = 0;
1898 int clean_busy;
1899
1900 if (event->status & DEPEVT_STATUS_BUSERR)
1901 status = -ECONNRESET;
1902
Paul Zimmerman1d046792012-02-15 18:56:56 -08001903 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001904 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001905 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001906
1907 /*
1908 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1909 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1910 */
1911 if (dwc->revision < DWC3_REVISION_183A) {
1912 u32 reg;
1913 int i;
1914
1915 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05001916 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03001917
1918 if (!(dep->flags & DWC3_EP_ENABLED))
1919 continue;
1920
1921 if (!list_empty(&dep->req_queued))
1922 return;
1923 }
1924
1925 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1926 reg |= dwc->u1u2;
1927 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1928
1929 dwc->u1u2 = 0;
1930 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001931}
1932
Felipe Balbi72246da2011-08-19 18:10:58 +03001933static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1934 const struct dwc3_event_depevt *event)
1935{
1936 struct dwc3_ep *dep;
1937 u8 epnum = event->endpoint_number;
1938
1939 dep = dwc->eps[epnum];
1940
Felipe Balbi3336abb2012-06-06 09:19:35 +03001941 if (!(dep->flags & DWC3_EP_ENABLED))
1942 return;
1943
Felipe Balbi72246da2011-08-19 18:10:58 +03001944 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1945 dwc3_ep_event_string(event->endpoint_event));
1946
1947 if (epnum == 0 || epnum == 1) {
1948 dwc3_ep0_interrupt(dwc, event);
1949 return;
1950 }
1951
1952 switch (event->endpoint_event) {
1953 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03001954 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001955
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001956 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001957 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1958 dep->name);
1959 return;
1960 }
1961
1962 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1963 break;
1964 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001965 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001966 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1967 dep->name);
1968 return;
1969 }
1970
1971 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1972 break;
1973 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001974 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001975 dwc3_gadget_start_isoc(dwc, dep, event);
1976 } else {
1977 int ret;
1978
1979 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001980 dep->name, event->status &
1981 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001982 ? "Transfer Active"
1983 : "Transfer Not Active");
1984
1985 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1986 if (!ret || ret == -EBUSY)
1987 return;
1988
1989 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1990 dep->name);
1991 }
1992
1993 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001994 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001995 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03001996 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1997 dep->name);
1998 return;
1999 }
2000
2001 switch (event->status) {
2002 case DEPEVT_STREAMEVT_FOUND:
2003 dev_vdbg(dwc->dev, "Stream %d found and started\n",
2004 event->parameters);
2005
2006 break;
2007 case DEPEVT_STREAMEVT_NOTFOUND:
2008 /* FALLTHROUGH */
2009 default:
2010 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
2011 }
2012 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002013 case DWC3_DEPEVT_RXTXFIFOEVT:
2014 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
2015 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002016 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbiea53b882012-02-17 12:10:04 +02002017 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002018 break;
2019 }
2020}
2021
2022static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2023{
2024 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2025 spin_unlock(&dwc->lock);
2026 dwc->gadget_driver->disconnect(&dwc->gadget);
2027 spin_lock(&dwc->lock);
2028 }
2029}
2030
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002031static void dwc3_suspend_gadget(struct dwc3 *dwc)
2032{
2033 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2034 spin_unlock(&dwc->lock);
2035 dwc->gadget_driver->suspend(&dwc->gadget);
2036 spin_lock(&dwc->lock);
2037 }
2038}
2039
2040static void dwc3_resume_gadget(struct dwc3 *dwc)
2041{
2042 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2043 spin_unlock(&dwc->lock);
2044 dwc->gadget_driver->resume(&dwc->gadget);
2045 spin_lock(&dwc->lock);
2046 }
2047}
2048
Paul Zimmermanb992e682012-04-27 14:17:35 +03002049static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002050{
2051 struct dwc3_ep *dep;
2052 struct dwc3_gadget_ep_cmd_params params;
2053 u32 cmd;
2054 int ret;
2055
2056 dep = dwc->eps[epnum];
2057
Felipe Balbib4996a82012-06-06 12:04:13 +03002058 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302059 return;
2060
Pratyush Anand57911502012-07-06 15:19:10 +05302061 /*
2062 * NOTICE: We are violating what the Databook says about the
2063 * EndTransfer command. Ideally we would _always_ wait for the
2064 * EndTransfer Command Completion IRQ, but that's causing too
2065 * much trouble synchronizing between us and gadget driver.
2066 *
2067 * We have discussed this with the IP Provider and it was
2068 * suggested to giveback all requests here, but give HW some
2069 * extra time to synchronize with the interconnect. We're using
2070 * an arbitraty 100us delay for that.
2071 *
2072 * Note also that a similar handling was tested by Synopsys
2073 * (thanks a lot Paul) and nothing bad has come out of it.
2074 * In short, what we're doing is:
2075 *
2076 * - Issue EndTransfer WITH CMDIOC bit set
2077 * - Wait 100us
2078 */
2079
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302080 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002081 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2082 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002083 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302084 memset(&params, 0, sizeof(params));
2085 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2086 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002087 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002088 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302089 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002090}
2091
2092static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2093{
2094 u32 epnum;
2095
2096 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2097 struct dwc3_ep *dep;
2098
2099 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002100 if (!dep)
2101 continue;
2102
Felipe Balbi72246da2011-08-19 18:10:58 +03002103 if (!(dep->flags & DWC3_EP_ENABLED))
2104 continue;
2105
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002106 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002107 }
2108}
2109
2110static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2111{
2112 u32 epnum;
2113
2114 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2115 struct dwc3_ep *dep;
2116 struct dwc3_gadget_ep_cmd_params params;
2117 int ret;
2118
2119 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002120 if (!dep)
2121 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002122
2123 if (!(dep->flags & DWC3_EP_STALL))
2124 continue;
2125
2126 dep->flags &= ~DWC3_EP_STALL;
2127
2128 memset(&params, 0, sizeof(params));
2129 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2130 DWC3_DEPCMD_CLEARSTALL, &params);
2131 WARN_ON_ONCE(ret);
2132 }
2133}
2134
2135static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2136{
Felipe Balbic4430a22012-05-24 10:30:01 +03002137 int reg;
2138
Felipe Balbi72246da2011-08-19 18:10:58 +03002139 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002140
2141 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2142 reg &= ~DWC3_DCTL_INITU1ENA;
2143 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2144
2145 reg &= ~DWC3_DCTL_INITU2ENA;
2146 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002147
Felipe Balbi72246da2011-08-19 18:10:58 +03002148 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002149 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002150
2151 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002152 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002153}
2154
Felipe Balbi72246da2011-08-19 18:10:58 +03002155static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2156{
2157 u32 reg;
2158
2159 dev_vdbg(dwc->dev, "%s\n", __func__);
2160
Felipe Balbidf62df52011-10-14 15:11:49 +03002161 /*
2162 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2163 * would cause a missing Disconnect Event if there's a
2164 * pending Setup Packet in the FIFO.
2165 *
2166 * There's no suggested workaround on the official Bug
2167 * report, which states that "unless the driver/application
2168 * is doing any special handling of a disconnect event,
2169 * there is no functional issue".
2170 *
2171 * Unfortunately, it turns out that we _do_ some special
2172 * handling of a disconnect event, namely complete all
2173 * pending transfers, notify gadget driver of the
2174 * disconnection, and so on.
2175 *
2176 * Our suggested workaround is to follow the Disconnect
2177 * Event steps here, instead, based on a setup_packet_pending
2178 * flag. Such flag gets set whenever we have a XferNotReady
2179 * event on EP0 and gets cleared on XferComplete for the
2180 * same endpoint.
2181 *
2182 * Refers to:
2183 *
2184 * STAR#9000466709: RTL: Device : Disconnect event not
2185 * generated if setup packet pending in FIFO
2186 */
2187 if (dwc->revision < DWC3_REVISION_188A) {
2188 if (dwc->setup_packet_pending)
2189 dwc3_gadget_disconnect_interrupt(dwc);
2190 }
2191
Felipe Balbi961906e2011-12-20 15:37:21 +02002192 /* after reset -> Default State */
Felipe Balbi14cd5922011-12-19 13:01:52 +02002193 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
Felipe Balbi961906e2011-12-20 15:37:21 +02002194
Felipe Balbi72246da2011-08-19 18:10:58 +03002195 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2196 dwc3_disconnect_gadget(dwc);
2197
2198 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2199 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2200 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002201 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002202
2203 dwc3_stop_active_transfers(dwc);
2204 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002205 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002206
2207 /* Reset device address to zero */
2208 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2209 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2210 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002211}
2212
2213static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2214{
2215 u32 reg;
2216 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2217
2218 /*
2219 * We change the clock only at SS but I dunno why I would want to do
2220 * this. Maybe it becomes part of the power saving plan.
2221 */
2222
2223 if (speed != DWC3_DSTS_SUPERSPEED)
2224 return;
2225
2226 /*
2227 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2228 * each time on Connect Done.
2229 */
2230 if (!usb30_clock)
2231 return;
2232
2233 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2234 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2235 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2236}
2237
Felipe Balbi72246da2011-08-19 18:10:58 +03002238static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2239{
Felipe Balbi72246da2011-08-19 18:10:58 +03002240 struct dwc3_ep *dep;
2241 int ret;
2242 u32 reg;
2243 u8 speed;
2244
2245 dev_vdbg(dwc->dev, "%s\n", __func__);
2246
Felipe Balbi72246da2011-08-19 18:10:58 +03002247 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2248 speed = reg & DWC3_DSTS_CONNECTSPD;
2249 dwc->speed = speed;
2250
2251 dwc3_update_ram_clk_sel(dwc, speed);
2252
2253 switch (speed) {
2254 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002255 /*
2256 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2257 * would cause a missing USB3 Reset event.
2258 *
2259 * In such situations, we should force a USB3 Reset
2260 * event by calling our dwc3_gadget_reset_interrupt()
2261 * routine.
2262 *
2263 * Refers to:
2264 *
2265 * STAR#9000483510: RTL: SS : USB3 reset event may
2266 * not be generated always when the link enters poll
2267 */
2268 if (dwc->revision < DWC3_REVISION_190A)
2269 dwc3_gadget_reset_interrupt(dwc);
2270
Felipe Balbi72246da2011-08-19 18:10:58 +03002271 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2272 dwc->gadget.ep0->maxpacket = 512;
2273 dwc->gadget.speed = USB_SPEED_SUPER;
2274 break;
2275 case DWC3_DCFG_HIGHSPEED:
2276 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2277 dwc->gadget.ep0->maxpacket = 64;
2278 dwc->gadget.speed = USB_SPEED_HIGH;
2279 break;
2280 case DWC3_DCFG_FULLSPEED2:
2281 case DWC3_DCFG_FULLSPEED1:
2282 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2283 dwc->gadget.ep0->maxpacket = 64;
2284 dwc->gadget.speed = USB_SPEED_FULL;
2285 break;
2286 case DWC3_DCFG_LOWSPEED:
2287 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2288 dwc->gadget.ep0->maxpacket = 8;
2289 dwc->gadget.speed = USB_SPEED_LOW;
2290 break;
2291 }
2292
Pratyush Anand2b758352013-01-14 15:59:31 +05302293 /* Enable USB2 LPM Capability */
2294
2295 if ((dwc->revision > DWC3_REVISION_194A)
2296 && (speed != DWC3_DCFG_SUPERSPEED)) {
2297 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2298 reg |= DWC3_DCFG_LPM_CAP;
2299 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2300
2301 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2302 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2303
Felipe Balbi1a947742013-01-24 11:56:11 +02002304 /*
2305 * TODO: This should be configurable. For now using
2306 * maximum allowed HIRD threshold value of 0b1100
2307 */
2308 reg |= DWC3_DCTL_HIRD_THRES(12);
Pratyush Anand2b758352013-01-14 15:59:31 +05302309
2310 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002311 } else {
2312 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2313 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2314 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302315 }
2316
Felipe Balbi72246da2011-08-19 18:10:58 +03002317 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002318 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2319 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002320 if (ret) {
2321 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2322 return;
2323 }
2324
2325 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002326 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2327 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002328 if (ret) {
2329 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2330 return;
2331 }
2332
2333 /*
2334 * Configure PHY via GUSB3PIPECTLn if required.
2335 *
2336 * Update GTXFIFOSIZn
2337 *
2338 * In both cases reset values should be sufficient.
2339 */
2340}
2341
2342static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2343{
2344 dev_vdbg(dwc->dev, "%s\n", __func__);
2345
2346 /*
2347 * TODO take core out of low power mode when that's
2348 * implemented.
2349 */
2350
2351 dwc->gadget_driver->resume(&dwc->gadget);
2352}
2353
2354static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2355 unsigned int evtinfo)
2356{
Felipe Balbifae2b902011-10-14 13:00:30 +03002357 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002358 unsigned int pwropt;
2359
2360 /*
2361 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2362 * Hibernation mode enabled which would show up when device detects
2363 * host-initiated U3 exit.
2364 *
2365 * In that case, device will generate a Link State Change Interrupt
2366 * from U3 to RESUME which is only necessary if Hibernation is
2367 * configured in.
2368 *
2369 * There are no functional changes due to such spurious event and we
2370 * just need to ignore it.
2371 *
2372 * Refers to:
2373 *
2374 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2375 * operational mode
2376 */
2377 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2378 if ((dwc->revision < DWC3_REVISION_250A) &&
2379 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2380 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2381 (next == DWC3_LINK_STATE_RESUME)) {
2382 dev_vdbg(dwc->dev, "ignoring transition U3 -> Resume\n");
2383 return;
2384 }
2385 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002386
2387 /*
2388 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2389 * on the link partner, the USB session might do multiple entry/exit
2390 * of low power states before a transfer takes place.
2391 *
2392 * Due to this problem, we might experience lower throughput. The
2393 * suggested workaround is to disable DCTL[12:9] bits if we're
2394 * transitioning from U1/U2 to U0 and enable those bits again
2395 * after a transfer completes and there are no pending transfers
2396 * on any of the enabled endpoints.
2397 *
2398 * This is the first half of that workaround.
2399 *
2400 * Refers to:
2401 *
2402 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2403 * core send LGO_Ux entering U0
2404 */
2405 if (dwc->revision < DWC3_REVISION_183A) {
2406 if (next == DWC3_LINK_STATE_U0) {
2407 u32 u1u2;
2408 u32 reg;
2409
2410 switch (dwc->link_state) {
2411 case DWC3_LINK_STATE_U1:
2412 case DWC3_LINK_STATE_U2:
2413 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2414 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2415 | DWC3_DCTL_ACCEPTU2ENA
2416 | DWC3_DCTL_INITU1ENA
2417 | DWC3_DCTL_ACCEPTU1ENA);
2418
2419 if (!dwc->u1u2)
2420 dwc->u1u2 = reg & u1u2;
2421
2422 reg &= ~u1u2;
2423
2424 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2425 break;
2426 default:
2427 /* do nothing */
2428 break;
2429 }
2430 }
2431 }
2432
2433 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002434
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002435 switch (next) {
2436 case DWC3_LINK_STATE_U1:
2437 if (dwc->speed == USB_SPEED_SUPER)
2438 dwc3_suspend_gadget(dwc);
2439 break;
2440 case DWC3_LINK_STATE_U2:
2441 case DWC3_LINK_STATE_U3:
2442 dwc3_suspend_gadget(dwc);
2443 break;
2444 case DWC3_LINK_STATE_RESUME:
2445 dwc3_resume_gadget(dwc);
2446 break;
2447 default:
2448 /* do nothing */
2449 break;
2450 }
2451
Felipe Balbi019ac832011-09-08 21:18:47 +03002452 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002453}
2454
Felipe Balbie1dadd32014-02-25 14:47:54 -06002455static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2456 unsigned int evtinfo)
2457{
2458 unsigned int is_ss = evtinfo & BIT(4);
2459
2460 /**
2461 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2462 * have a known issue which can cause USB CV TD.9.23 to fail
2463 * randomly.
2464 *
2465 * Because of this issue, core could generate bogus hibernation
2466 * events which SW needs to ignore.
2467 *
2468 * Refers to:
2469 *
2470 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2471 * Device Fallback from SuperSpeed
2472 */
2473 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2474 return;
2475
2476 /* enter hibernation here */
2477}
2478
Felipe Balbi72246da2011-08-19 18:10:58 +03002479static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2480 const struct dwc3_event_devt *event)
2481{
2482 switch (event->type) {
2483 case DWC3_DEVICE_EVENT_DISCONNECT:
2484 dwc3_gadget_disconnect_interrupt(dwc);
2485 break;
2486 case DWC3_DEVICE_EVENT_RESET:
2487 dwc3_gadget_reset_interrupt(dwc);
2488 break;
2489 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2490 dwc3_gadget_conndone_interrupt(dwc);
2491 break;
2492 case DWC3_DEVICE_EVENT_WAKEUP:
2493 dwc3_gadget_wakeup_interrupt(dwc);
2494 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002495 case DWC3_DEVICE_EVENT_HIBER_REQ:
2496 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2497 "unexpected hibernation event\n"))
2498 break;
2499
2500 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2501 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002502 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2503 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2504 break;
2505 case DWC3_DEVICE_EVENT_EOPF:
2506 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2507 break;
2508 case DWC3_DEVICE_EVENT_SOF:
2509 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2510 break;
2511 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2512 dev_vdbg(dwc->dev, "Erratic Error\n");
2513 break;
2514 case DWC3_DEVICE_EVENT_CMD_CMPL:
2515 dev_vdbg(dwc->dev, "Command Complete\n");
2516 break;
2517 case DWC3_DEVICE_EVENT_OVERFLOW:
2518 dev_vdbg(dwc->dev, "Overflow\n");
2519 break;
2520 default:
2521 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2522 }
2523}
2524
2525static void dwc3_process_event_entry(struct dwc3 *dwc,
2526 const union dwc3_event *event)
2527{
2528 /* Endpoint IRQ, handle it and return early */
2529 if (event->type.is_devspec == 0) {
2530 /* depevt */
2531 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2532 }
2533
2534 switch (event->type.type) {
2535 case DWC3_EVENT_TYPE_DEV:
2536 dwc3_gadget_interrupt(dwc, &event->devt);
2537 break;
2538 /* REVISIT what to do with Carkit and I2C events ? */
2539 default:
2540 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2541 }
2542}
2543
Felipe Balbif42f2442013-06-12 21:25:08 +03002544static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2545{
2546 struct dwc3_event_buffer *evt;
2547 irqreturn_t ret = IRQ_NONE;
2548 int left;
2549 u32 reg;
2550
2551 evt = dwc->ev_buffs[buf];
2552 left = evt->count;
2553
2554 if (!(evt->flags & DWC3_EVENT_PENDING))
2555 return IRQ_NONE;
2556
2557 while (left > 0) {
2558 union dwc3_event event;
2559
2560 event.raw = *(u32 *) (evt->buf + evt->lpos);
2561
2562 dwc3_process_event_entry(dwc, &event);
2563
2564 /*
2565 * FIXME we wrap around correctly to the next entry as
2566 * almost all entries are 4 bytes in size. There is one
2567 * entry which has 12 bytes which is a regular entry
2568 * followed by 8 bytes data. ATM I don't know how
2569 * things are organized if we get next to the a
2570 * boundary so I worry about that once we try to handle
2571 * that.
2572 */
2573 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2574 left -= 4;
2575
2576 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2577 }
2578
2579 evt->count = 0;
2580 evt->flags &= ~DWC3_EVENT_PENDING;
2581 ret = IRQ_HANDLED;
2582
2583 /* Unmask interrupt */
2584 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2585 reg &= ~DWC3_GEVNTSIZ_INTMASK;
2586 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2587
2588 return ret;
2589}
2590
Felipe Balbib15a7622011-06-30 16:57:15 +03002591static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
2592{
2593 struct dwc3 *dwc = _dwc;
2594 unsigned long flags;
2595 irqreturn_t ret = IRQ_NONE;
2596 int i;
2597
2598 spin_lock_irqsave(&dwc->lock, flags);
2599
Felipe Balbif42f2442013-06-12 21:25:08 +03002600 for (i = 0; i < dwc->num_event_buffers; i++)
2601 ret |= dwc3_process_event_buf(dwc, i);
Felipe Balbib15a7622011-06-30 16:57:15 +03002602
2603 spin_unlock_irqrestore(&dwc->lock, flags);
2604
2605 return ret;
2606}
2607
Felipe Balbi7f97aa92013-06-12 21:16:11 +03002608static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc, u32 buf)
Felipe Balbi72246da2011-08-19 18:10:58 +03002609{
2610 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002611 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002612 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002613
Felipe Balbib15a7622011-06-30 16:57:15 +03002614 evt = dwc->ev_buffs[buf];
2615
Felipe Balbi72246da2011-08-19 18:10:58 +03002616 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2617 count &= DWC3_GEVNTCOUNT_MASK;
2618 if (!count)
2619 return IRQ_NONE;
2620
Felipe Balbib15a7622011-06-30 16:57:15 +03002621 evt->count = count;
2622 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002623
Felipe Balbie8adfc32013-06-12 21:11:14 +03002624 /* Mask interrupt */
2625 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2626 reg |= DWC3_GEVNTSIZ_INTMASK;
2627 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2628
Felipe Balbib15a7622011-06-30 16:57:15 +03002629 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002630}
2631
2632static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2633{
2634 struct dwc3 *dwc = _dwc;
2635 int i;
2636 irqreturn_t ret = IRQ_NONE;
2637
2638 spin_lock(&dwc->lock);
2639
Felipe Balbi9f622b22011-10-12 10:31:04 +03002640 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002641 irqreturn_t status;
2642
Felipe Balbi7f97aa92013-06-12 21:16:11 +03002643 status = dwc3_check_event_buf(dwc, i);
Felipe Balbib15a7622011-06-30 16:57:15 +03002644 if (status == IRQ_WAKE_THREAD)
Felipe Balbi72246da2011-08-19 18:10:58 +03002645 ret = status;
2646 }
2647
2648 spin_unlock(&dwc->lock);
2649
2650 return ret;
2651}
2652
2653/**
2654 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002655 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002656 *
2657 * Returns 0 on success otherwise negative errno.
2658 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002659int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002660{
Felipe Balbi72246da2011-08-19 18:10:58 +03002661 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002662
2663 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2664 &dwc->ctrl_req_addr, GFP_KERNEL);
2665 if (!dwc->ctrl_req) {
2666 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2667 ret = -ENOMEM;
2668 goto err0;
2669 }
2670
2671 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2672 &dwc->ep0_trb_addr, GFP_KERNEL);
2673 if (!dwc->ep0_trb) {
2674 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2675 ret = -ENOMEM;
2676 goto err1;
2677 }
2678
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002679 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002680 if (!dwc->setup_buf) {
2681 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2682 ret = -ENOMEM;
2683 goto err2;
2684 }
2685
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002686 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002687 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2688 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002689 if (!dwc->ep0_bounce) {
2690 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2691 ret = -ENOMEM;
2692 goto err3;
2693 }
2694
Felipe Balbi72246da2011-08-19 18:10:58 +03002695 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002696 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002697 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002698 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002699 dwc->gadget.name = "dwc3-gadget";
2700
2701 /*
David Cohena4b9d942013-12-09 15:55:38 -08002702 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2703 * on ep out.
2704 */
2705 dwc->gadget.quirk_ep_out_aligned_size = true;
2706
2707 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002708 * REVISIT: Here we should clear all pending IRQs to be
2709 * sure we're starting from a well known location.
2710 */
2711
2712 ret = dwc3_gadget_init_endpoints(dwc);
2713 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002714 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002715
Felipe Balbi72246da2011-08-19 18:10:58 +03002716 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2717 if (ret) {
2718 dev_err(dwc->dev, "failed to register udc\n");
David Cohene1f80462013-09-11 17:42:47 -07002719 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002720 }
2721
2722 return 0;
2723
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002724err4:
David Cohene1f80462013-09-11 17:42:47 -07002725 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002726 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2727 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002728
Felipe Balbi72246da2011-08-19 18:10:58 +03002729err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002730 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002731
2732err2:
2733 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2734 dwc->ep0_trb, dwc->ep0_trb_addr);
2735
2736err1:
2737 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2738 dwc->ctrl_req, dwc->ctrl_req_addr);
2739
2740err0:
2741 return ret;
2742}
2743
Felipe Balbi7415f172012-04-30 14:56:33 +03002744/* -------------------------------------------------------------------------- */
2745
Felipe Balbi72246da2011-08-19 18:10:58 +03002746void dwc3_gadget_exit(struct dwc3 *dwc)
2747{
Felipe Balbi72246da2011-08-19 18:10:58 +03002748 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002749
Felipe Balbi72246da2011-08-19 18:10:58 +03002750 dwc3_gadget_free_endpoints(dwc);
2751
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002752 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2753 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002754
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002755 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002756
2757 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2758 dwc->ep0_trb, dwc->ep0_trb_addr);
2759
2760 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2761 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002762}
Felipe Balbi7415f172012-04-30 14:56:33 +03002763
2764int dwc3_gadget_prepare(struct dwc3 *dwc)
2765{
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002766 if (dwc->pullups_connected) {
Felipe Balbi7415f172012-04-30 14:56:33 +03002767 dwc3_gadget_disable_irq(dwc);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002768 dwc3_gadget_run_stop(dwc, true, true);
2769 }
Felipe Balbi7415f172012-04-30 14:56:33 +03002770
2771 return 0;
2772}
2773
2774void dwc3_gadget_complete(struct dwc3 *dwc)
2775{
2776 if (dwc->pullups_connected) {
2777 dwc3_gadget_enable_irq(dwc);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002778 dwc3_gadget_run_stop(dwc, true, false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002779 }
2780}
2781
2782int dwc3_gadget_suspend(struct dwc3 *dwc)
2783{
2784 __dwc3_gadget_ep_disable(dwc->eps[0]);
2785 __dwc3_gadget_ep_disable(dwc->eps[1]);
2786
2787 dwc->dcfg = dwc3_readl(dwc->regs, DWC3_DCFG);
2788
2789 return 0;
2790}
2791
2792int dwc3_gadget_resume(struct dwc3 *dwc)
2793{
2794 struct dwc3_ep *dep;
2795 int ret;
2796
2797 /* Start with SuperSpeed Default */
2798 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2799
2800 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002801 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2802 false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002803 if (ret)
2804 goto err0;
2805
2806 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002807 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2808 false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002809 if (ret)
2810 goto err1;
2811
2812 /* begin to receive SETUP packets */
2813 dwc->ep0state = EP0_SETUP_PHASE;
2814 dwc3_ep0_out_start(dwc);
2815
2816 dwc3_writel(dwc->regs, DWC3_DCFG, dwc->dcfg);
2817
2818 return 0;
2819
2820err1:
2821 __dwc3_gadget_ep_disable(dwc->eps[0]);
2822
2823err0:
2824 return ret;
2825}