usb: dwc2: gadget: Remove unnecessary code
[linux-block.git] / drivers / usb / dwc2 / gadget.c
CommitLineData
8b9bc460 1/**
dfbc6fa3
AT
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
5b7d70c6
BD
4 *
5 * Copyright 2008 Openmoko, Inc.
6 * Copyright 2008 Simtec Electronics
7 * Ben Dooks <ben@simtec.co.uk>
8 * http://armlinux.simtec.co.uk/
9 *
10 * S3C USB2.0 High-speed / OtG driver
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
8b9bc460 15 */
5b7d70c6
BD
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/spinlock.h>
20#include <linux/interrupt.h>
21#include <linux/platform_device.h>
22#include <linux/dma-mapping.h>
7ad8096e 23#include <linux/mutex.h>
5b7d70c6
BD
24#include <linux/seq_file.h>
25#include <linux/delay.h>
26#include <linux/io.h>
5a0e3ad6 27#include <linux/slab.h>
c50f056c 28#include <linux/of_platform.h>
5b7d70c6
BD
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
b2e587db 32#include <linux/usb/phy.h>
5b7d70c6 33
f7c0b143 34#include "core.h"
941fcce4 35#include "hw.h"
5b7d70c6
BD
36
37/* conversion functions */
1f91b4cc 38static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
5b7d70c6 39{
1f91b4cc 40 return container_of(req, struct dwc2_hsotg_req, req);
5b7d70c6
BD
41}
42
1f91b4cc 43static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
5b7d70c6 44{
1f91b4cc 45 return container_of(ep, struct dwc2_hsotg_ep, ep);
5b7d70c6
BD
46}
47
941fcce4 48static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
5b7d70c6 49{
941fcce4 50 return container_of(gadget, struct dwc2_hsotg, gadget);
5b7d70c6
BD
51}
52
53static inline void __orr32(void __iomem *ptr, u32 val)
54{
95c8bc36 55 dwc2_writel(dwc2_readl(ptr) | val, ptr);
5b7d70c6
BD
56}
57
58static inline void __bic32(void __iomem *ptr, u32 val)
59{
95c8bc36 60 dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
5b7d70c6
BD
61}
62
1f91b4cc 63static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
c6f5c050
MYK
64 u32 ep_index, u32 dir_in)
65{
66 if (dir_in)
67 return hsotg->eps_in[ep_index];
68 else
69 return hsotg->eps_out[ep_index];
70}
71
997f4f81 72/* forward declaration of functions */
1f91b4cc 73static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
5b7d70c6
BD
74
75/**
76 * using_dma - return the DMA status of the driver.
77 * @hsotg: The driver state.
78 *
79 * Return true if we're using DMA.
80 *
81 * Currently, we have the DMA support code worked into everywhere
82 * that needs it, but the AMBA DMA implementation in the hardware can
83 * only DMA from 32bit aligned addresses. This means that gadgets such
84 * as the CDC Ethernet cannot work as they often pass packets which are
85 * not 32bit aligned.
86 *
87 * Unfortunately the choice to use DMA or not is global to the controller
88 * and seems to be only settable when the controller is being put through
89 * a core reset. This means we either need to fix the gadgets to take
90 * account of DMA alignment, or add bounce buffers (yuerk).
91 *
edd74be8 92 * g_using_dma is set depending on dts flag.
5b7d70c6 93 */
941fcce4 94static inline bool using_dma(struct dwc2_hsotg *hsotg)
5b7d70c6 95{
edd74be8 96 return hsotg->g_using_dma;
5b7d70c6
BD
97}
98
99/**
1f91b4cc 100 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
5b7d70c6
BD
101 * @hsotg: The device state
102 * @ints: A bitmask of the interrupts to enable
103 */
1f91b4cc 104static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
5b7d70c6 105{
95c8bc36 106 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
5b7d70c6
BD
107 u32 new_gsintmsk;
108
109 new_gsintmsk = gsintmsk | ints;
110
111 if (new_gsintmsk != gsintmsk) {
112 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
95c8bc36 113 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
5b7d70c6
BD
114 }
115}
116
117/**
1f91b4cc 118 * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
5b7d70c6
BD
119 * @hsotg: The device state
120 * @ints: A bitmask of the interrupts to enable
121 */
1f91b4cc 122static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
5b7d70c6 123{
95c8bc36 124 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
5b7d70c6
BD
125 u32 new_gsintmsk;
126
127 new_gsintmsk = gsintmsk & ~ints;
128
129 if (new_gsintmsk != gsintmsk)
95c8bc36 130 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
5b7d70c6
BD
131}
132
133/**
1f91b4cc 134 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
5b7d70c6
BD
135 * @hsotg: The device state
136 * @ep: The endpoint index
137 * @dir_in: True if direction is in.
138 * @en: The enable value, true to enable
139 *
140 * Set or clear the mask for an individual endpoint's interrupt
141 * request.
142 */
1f91b4cc 143static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
144 unsigned int ep, unsigned int dir_in,
145 unsigned int en)
146{
147 unsigned long flags;
148 u32 bit = 1 << ep;
149 u32 daint;
150
151 if (!dir_in)
152 bit <<= 16;
153
154 local_irq_save(flags);
95c8bc36 155 daint = dwc2_readl(hsotg->regs + DAINTMSK);
5b7d70c6
BD
156 if (en)
157 daint |= bit;
158 else
159 daint &= ~bit;
95c8bc36 160 dwc2_writel(daint, hsotg->regs + DAINTMSK);
5b7d70c6
BD
161 local_irq_restore(flags);
162}
163
164/**
1f91b4cc 165 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
5b7d70c6
BD
166 * @hsotg: The device instance.
167 */
1f91b4cc 168static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
5b7d70c6 169{
0f002d20
BD
170 unsigned int ep;
171 unsigned int addr;
1703a6d3 172 int timeout;
0f002d20
BD
173 u32 val;
174
7fcbc95c
GH
175 /* Reset fifo map if not correctly cleared during previous session */
176 WARN_ON(hsotg->fifo_map);
177 hsotg->fifo_map = 0;
178
0a176279 179 /* set RX/NPTX FIFO sizes */
95c8bc36
AS
180 dwc2_writel(hsotg->g_rx_fifo_sz, hsotg->regs + GRXFSIZ);
181 dwc2_writel((hsotg->g_rx_fifo_sz << FIFOSIZE_STARTADDR_SHIFT) |
0a176279
GH
182 (hsotg->g_np_g_tx_fifo_sz << FIFOSIZE_DEPTH_SHIFT),
183 hsotg->regs + GNPTXFSIZ);
0f002d20 184
8b9bc460
LM
185 /*
186 * arange all the rest of the TX FIFOs, as some versions of this
0f002d20
BD
187 * block have overlapping default addresses. This also ensures
188 * that if the settings have been changed, then they are set to
8b9bc460
LM
189 * known values.
190 */
0f002d20
BD
191
192 /* start at the end of the GNPTXFSIZ, rounded up */
0a176279 193 addr = hsotg->g_rx_fifo_sz + hsotg->g_np_g_tx_fifo_sz;
0f002d20 194
8b9bc460 195 /*
0a176279 196 * Configure fifos sizes from provided configuration and assign
b203d0a2
RB
197 * them to endpoints dynamically according to maxpacket size value of
198 * given endpoint.
8b9bc460 199 */
0a176279
GH
200 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
201 if (!hsotg->g_tx_fifo_sz[ep])
202 continue;
0f002d20 203 val = addr;
0a176279
GH
204 val |= hsotg->g_tx_fifo_sz[ep] << FIFOSIZE_DEPTH_SHIFT;
205 WARN_ONCE(addr + hsotg->g_tx_fifo_sz[ep] > hsotg->fifo_mem,
cff9eb75 206 "insufficient fifo memory");
0a176279 207 addr += hsotg->g_tx_fifo_sz[ep];
0f002d20 208
95c8bc36 209 dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
0f002d20 210 }
1703a6d3 211
8b9bc460
LM
212 /*
213 * according to p428 of the design guide, we need to ensure that
214 * all fifos are flushed before continuing
215 */
1703a6d3 216
95c8bc36 217 dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
47a1685f 218 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
1703a6d3
BD
219
220 /* wait until the fifos are both flushed */
221 timeout = 100;
222 while (1) {
95c8bc36 223 val = dwc2_readl(hsotg->regs + GRSTCTL);
1703a6d3 224
47a1685f 225 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
1703a6d3
BD
226 break;
227
228 if (--timeout == 0) {
229 dev_err(hsotg->dev,
230 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
231 __func__, val);
48b20bcb 232 break;
1703a6d3
BD
233 }
234
235 udelay(1);
236 }
237
238 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
5b7d70c6
BD
239}
240
241/**
242 * @ep: USB endpoint to allocate request for.
243 * @flags: Allocation flags
244 *
245 * Allocate a new USB request structure appropriate for the specified endpoint
246 */
1f91b4cc 247static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
0978f8c5 248 gfp_t flags)
5b7d70c6 249{
1f91b4cc 250 struct dwc2_hsotg_req *req;
5b7d70c6 251
1f91b4cc 252 req = kzalloc(sizeof(struct dwc2_hsotg_req), flags);
5b7d70c6
BD
253 if (!req)
254 return NULL;
255
256 INIT_LIST_HEAD(&req->queue);
257
5b7d70c6
BD
258 return &req->req;
259}
260
261/**
262 * is_ep_periodic - return true if the endpoint is in periodic mode.
263 * @hs_ep: The endpoint to query.
264 *
265 * Returns true if the endpoint is in periodic mode, meaning it is being
266 * used for an Interrupt or ISO transfer.
267 */
1f91b4cc 268static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
5b7d70c6
BD
269{
270 return hs_ep->periodic;
271}
272
273/**
1f91b4cc 274 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
5b7d70c6
BD
275 * @hsotg: The device state.
276 * @hs_ep: The endpoint for the request
277 * @hs_req: The request being processed.
278 *
1f91b4cc 279 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
5b7d70c6 280 * of a request to ensure the buffer is ready for access by the caller.
8b9bc460 281 */
1f91b4cc
FB
282static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
283 struct dwc2_hsotg_ep *hs_ep,
284 struct dwc2_hsotg_req *hs_req)
5b7d70c6
BD
285{
286 struct usb_request *req = &hs_req->req;
5b7d70c6
BD
287
288 /* ignore this if we're not moving any data */
289 if (hs_req->req.length == 0)
290 return;
291
17d966a3 292 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
5b7d70c6
BD
293}
294
295/**
1f91b4cc 296 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
5b7d70c6
BD
297 * @hsotg: The controller state.
298 * @hs_ep: The endpoint we're going to write for.
299 * @hs_req: The request to write data for.
300 *
301 * This is called when the TxFIFO has some space in it to hold a new
302 * transmission and we have something to give it. The actual setup of
303 * the data size is done elsewhere, so all we have to do is to actually
304 * write the data.
305 *
306 * The return value is zero if there is more space (or nothing was done)
307 * otherwise -ENOSPC is returned if the FIFO space was used up.
308 *
309 * This routine is only needed for PIO
8b9bc460 310 */
1f91b4cc
FB
311static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
312 struct dwc2_hsotg_ep *hs_ep,
313 struct dwc2_hsotg_req *hs_req)
5b7d70c6
BD
314{
315 bool periodic = is_ep_periodic(hs_ep);
95c8bc36 316 u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
5b7d70c6
BD
317 int buf_pos = hs_req->req.actual;
318 int to_write = hs_ep->size_loaded;
319 void *data;
320 int can_write;
321 int pkt_round;
4fca54aa 322 int max_transfer;
5b7d70c6
BD
323
324 to_write -= (buf_pos - hs_ep->last_load);
325
326 /* if there's nothing to write, get out early */
327 if (to_write == 0)
328 return 0;
329
10aebc77 330 if (periodic && !hsotg->dedicated_fifos) {
95c8bc36 331 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
5b7d70c6
BD
332 int size_left;
333 int size_done;
334
8b9bc460
LM
335 /*
336 * work out how much data was loaded so we can calculate
337 * how much data is left in the fifo.
338 */
5b7d70c6 339
47a1685f 340 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
5b7d70c6 341
8b9bc460
LM
342 /*
343 * if shared fifo, we cannot write anything until the
e7a9ff54
BD
344 * previous data has been completely sent.
345 */
346 if (hs_ep->fifo_load != 0) {
1f91b4cc 347 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
e7a9ff54
BD
348 return -ENOSPC;
349 }
350
5b7d70c6
BD
351 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
352 __func__, size_left,
353 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
354
355 /* how much of the data has moved */
356 size_done = hs_ep->size_loaded - size_left;
357
358 /* how much data is left in the fifo */
359 can_write = hs_ep->fifo_load - size_done;
360 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
361 __func__, can_write);
362
363 can_write = hs_ep->fifo_size - can_write;
364 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
365 __func__, can_write);
366
367 if (can_write <= 0) {
1f91b4cc 368 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
5b7d70c6
BD
369 return -ENOSPC;
370 }
10aebc77 371 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
95c8bc36 372 can_write = dwc2_readl(hsotg->regs + DTXFSTS(hs_ep->index));
10aebc77
BD
373
374 can_write &= 0xffff;
375 can_write *= 4;
5b7d70c6 376 } else {
47a1685f 377 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
5b7d70c6
BD
378 dev_dbg(hsotg->dev,
379 "%s: no queue slots available (0x%08x)\n",
380 __func__, gnptxsts);
381
1f91b4cc 382 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
5b7d70c6
BD
383 return -ENOSPC;
384 }
385
47a1685f 386 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
679f9b7c 387 can_write *= 4; /* fifo size is in 32bit quantities. */
5b7d70c6
BD
388 }
389
4fca54aa
RB
390 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
391
392 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
393 __func__, gnptxsts, can_write, to_write, max_transfer);
5b7d70c6 394
8b9bc460
LM
395 /*
396 * limit to 512 bytes of data, it seems at least on the non-periodic
5b7d70c6
BD
397 * FIFO, requests of >512 cause the endpoint to get stuck with a
398 * fragment of the end of the transfer in it.
399 */
811f3303 400 if (can_write > 512 && !periodic)
5b7d70c6
BD
401 can_write = 512;
402
8b9bc460
LM
403 /*
404 * limit the write to one max-packet size worth of data, but allow
03e10e5a 405 * the transfer to return that it did not run out of fifo space
8b9bc460
LM
406 * doing it.
407 */
4fca54aa
RB
408 if (to_write > max_transfer) {
409 to_write = max_transfer;
03e10e5a 410
5cb2ff0c
RB
411 /* it's needed only when we do not use dedicated fifos */
412 if (!hsotg->dedicated_fifos)
1f91b4cc 413 dwc2_hsotg_en_gsint(hsotg,
47a1685f
DN
414 periodic ? GINTSTS_PTXFEMP :
415 GINTSTS_NPTXFEMP);
03e10e5a
BD
416 }
417
5b7d70c6
BD
418 /* see if we can write data */
419
420 if (to_write > can_write) {
421 to_write = can_write;
4fca54aa 422 pkt_round = to_write % max_transfer;
5b7d70c6 423
8b9bc460
LM
424 /*
425 * Round the write down to an
5b7d70c6
BD
426 * exact number of packets.
427 *
428 * Note, we do not currently check to see if we can ever
429 * write a full packet or not to the FIFO.
430 */
431
432 if (pkt_round)
433 to_write -= pkt_round;
434
8b9bc460
LM
435 /*
436 * enable correct FIFO interrupt to alert us when there
437 * is more room left.
438 */
5b7d70c6 439
5cb2ff0c
RB
440 /* it's needed only when we do not use dedicated fifos */
441 if (!hsotg->dedicated_fifos)
1f91b4cc 442 dwc2_hsotg_en_gsint(hsotg,
47a1685f
DN
443 periodic ? GINTSTS_PTXFEMP :
444 GINTSTS_NPTXFEMP);
5b7d70c6
BD
445 }
446
447 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
448 to_write, hs_req->req.length, can_write, buf_pos);
449
450 if (to_write <= 0)
451 return -ENOSPC;
452
453 hs_req->req.actual = buf_pos + to_write;
454 hs_ep->total_data += to_write;
455
456 if (periodic)
457 hs_ep->fifo_load += to_write;
458
459 to_write = DIV_ROUND_UP(to_write, 4);
460 data = hs_req->req.buf + buf_pos;
461
1a7ed5be 462 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
5b7d70c6
BD
463
464 return (to_write >= can_write) ? -ENOSPC : 0;
465}
466
467/**
468 * get_ep_limit - get the maximum data legnth for this endpoint
469 * @hs_ep: The endpoint
470 *
471 * Return the maximum data that can be queued in one go on a given endpoint
472 * so that transfers that are too long can be split.
473 */
1f91b4cc 474static unsigned get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
5b7d70c6
BD
475{
476 int index = hs_ep->index;
477 unsigned maxsize;
478 unsigned maxpkt;
479
480 if (index != 0) {
47a1685f
DN
481 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
482 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
5b7d70c6 483 } else {
b05ca580 484 maxsize = 64+64;
66e5c643 485 if (hs_ep->dir_in)
47a1685f 486 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
66e5c643 487 else
5b7d70c6 488 maxpkt = 2;
5b7d70c6
BD
489 }
490
491 /* we made the constant loading easier above by using +1 */
492 maxpkt--;
493 maxsize--;
494
8b9bc460
LM
495 /*
496 * constrain by packet count if maxpkts*pktsize is greater
497 * than the length register size.
498 */
5b7d70c6
BD
499
500 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
501 maxsize = maxpkt * hs_ep->ep.maxpacket;
502
503 return maxsize;
504}
505
506/**
1f91b4cc 507 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
5b7d70c6
BD
508 * @hsotg: The controller state.
509 * @hs_ep: The endpoint to process a request for
510 * @hs_req: The request to start.
511 * @continuing: True if we are doing more for the current request.
512 *
513 * Start the given request running by setting the endpoint registers
514 * appropriately, and writing any data to the FIFOs.
515 */
1f91b4cc
FB
516static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
517 struct dwc2_hsotg_ep *hs_ep,
518 struct dwc2_hsotg_req *hs_req,
5b7d70c6
BD
519 bool continuing)
520{
521 struct usb_request *ureq = &hs_req->req;
522 int index = hs_ep->index;
523 int dir_in = hs_ep->dir_in;
524 u32 epctrl_reg;
525 u32 epsize_reg;
526 u32 epsize;
527 u32 ctrl;
528 unsigned length;
529 unsigned packets;
530 unsigned maxreq;
531
532 if (index != 0) {
533 if (hs_ep->req && !continuing) {
534 dev_err(hsotg->dev, "%s: active request\n", __func__);
535 WARN_ON(1);
536 return;
537 } else if (hs_ep->req != hs_req && continuing) {
538 dev_err(hsotg->dev,
539 "%s: continue different req\n", __func__);
540 WARN_ON(1);
541 return;
542 }
543 }
544
94cb8fd6
LM
545 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
546 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
5b7d70c6
BD
547
548 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
95c8bc36 549 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
5b7d70c6
BD
550 hs_ep->dir_in ? "in" : "out");
551
9c39ddc6 552 /* If endpoint is stalled, we will restart request later */
95c8bc36 553 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
9c39ddc6 554
b2d4c54e 555 if (index && ctrl & DXEPCTL_STALL) {
9c39ddc6
AT
556 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
557 return;
558 }
559
5b7d70c6 560 length = ureq->length - ureq->actual;
71225bee
LM
561 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
562 ureq->length, ureq->actual);
5b7d70c6
BD
563
564 maxreq = get_ep_limit(hs_ep);
565 if (length > maxreq) {
566 int round = maxreq % hs_ep->ep.maxpacket;
567
568 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
569 __func__, length, maxreq, round);
570
571 /* round down to multiple of packets */
572 if (round)
573 maxreq -= round;
574
575 length = maxreq;
576 }
577
578 if (length)
579 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
580 else
581 packets = 1; /* send one packet if length is zero. */
582
4fca54aa
RB
583 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
584 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
585 return;
586 }
587
5b7d70c6 588 if (dir_in && index != 0)
4fca54aa 589 if (hs_ep->isochronous)
47a1685f 590 epsize = DXEPTSIZ_MC(packets);
4fca54aa 591 else
47a1685f 592 epsize = DXEPTSIZ_MC(1);
5b7d70c6
BD
593 else
594 epsize = 0;
595
f71b5e25
MYK
596 /*
597 * zero length packet should be programmed on its own and should not
598 * be counted in DIEPTSIZ.PktCnt with other packets.
599 */
600 if (dir_in && ureq->zero && !continuing) {
601 /* Test if zlp is actually required. */
602 if ((ureq->length >= hs_ep->ep.maxpacket) &&
603 !(ureq->length % hs_ep->ep.maxpacket))
8a20fa45 604 hs_ep->send_zlp = 1;
5b7d70c6
BD
605 }
606
47a1685f
DN
607 epsize |= DXEPTSIZ_PKTCNT(packets);
608 epsize |= DXEPTSIZ_XFERSIZE(length);
5b7d70c6
BD
609
610 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
611 __func__, packets, length, ureq->length, epsize, epsize_reg);
612
613 /* store the request as the current one we're doing */
614 hs_ep->req = hs_req;
615
616 /* write size / packets */
95c8bc36 617 dwc2_writel(epsize, hsotg->regs + epsize_reg);
5b7d70c6 618
db1d8ba3 619 if (using_dma(hsotg) && !continuing) {
5b7d70c6
BD
620 unsigned int dma_reg;
621
8b9bc460
LM
622 /*
623 * write DMA address to control register, buffer already
1f91b4cc 624 * synced by dwc2_hsotg_ep_queue().
8b9bc460 625 */
5b7d70c6 626
94cb8fd6 627 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
95c8bc36 628 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
5b7d70c6 629
0cc4cf6f 630 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
8b3bc14f 631 __func__, &ureq->dma, dma_reg);
5b7d70c6
BD
632 }
633
47a1685f 634 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
71225bee 635
fe0b94ab 636 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
71225bee
LM
637
638 /* For Setup request do not clear NAK */
fe0b94ab 639 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
47a1685f 640 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
71225bee 641
5b7d70c6 642 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
95c8bc36 643 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
5b7d70c6 644
8b9bc460
LM
645 /*
646 * set these, it seems that DMA support increments past the end
5b7d70c6 647 * of the packet buffer so we need to calculate the length from
8b9bc460
LM
648 * this information.
649 */
5b7d70c6
BD
650 hs_ep->size_loaded = length;
651 hs_ep->last_load = ureq->actual;
652
653 if (dir_in && !using_dma(hsotg)) {
654 /* set these anyway, we may need them for non-periodic in */
655 hs_ep->fifo_load = 0;
656
1f91b4cc 657 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
5b7d70c6
BD
658 }
659
8b9bc460
LM
660 /*
661 * Note, trying to clear the NAK here causes problems with transmit
662 * on the S3C6400 ending up with the TXFIFO becoming full.
663 */
5b7d70c6
BD
664
665 /* check ep is enabled */
95c8bc36 666 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
1a0ed863 667 dev_dbg(hsotg->dev,
47a1685f 668 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
95c8bc36 669 index, dwc2_readl(hsotg->regs + epctrl_reg));
5b7d70c6 670
47a1685f 671 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
95c8bc36 672 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
afcf4169
RB
673
674 /* enable ep interrupts */
1f91b4cc 675 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
5b7d70c6
BD
676}
677
678/**
1f91b4cc 679 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
5b7d70c6
BD
680 * @hsotg: The device state.
681 * @hs_ep: The endpoint the request is on.
682 * @req: The request being processed.
683 *
684 * We've been asked to queue a request, so ensure that the memory buffer
685 * is correctly setup for DMA. If we've been passed an extant DMA address
686 * then ensure the buffer has been synced to memory. If our buffer has no
687 * DMA memory, then we map the memory and mark our request to allow us to
688 * cleanup on completion.
8b9bc460 689 */
1f91b4cc
FB
690static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
691 struct dwc2_hsotg_ep *hs_ep,
5b7d70c6
BD
692 struct usb_request *req)
693{
1f91b4cc 694 struct dwc2_hsotg_req *hs_req = our_req(req);
e58ebcd1 695 int ret;
5b7d70c6
BD
696
697 /* if the length is zero, ignore the DMA data */
698 if (hs_req->req.length == 0)
699 return 0;
700
e58ebcd1
FB
701 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
702 if (ret)
703 goto dma_error;
5b7d70c6
BD
704
705 return 0;
706
707dma_error:
708 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
709 __func__, req->buf, req->length);
710
711 return -EIO;
712}
713
1f91b4cc
FB
714static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
715 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
7d24c1b5
MYK
716{
717 void *req_buf = hs_req->req.buf;
718
719 /* If dma is not being used or buffer is aligned */
720 if (!using_dma(hsotg) || !((long)req_buf & 3))
721 return 0;
722
723 WARN_ON(hs_req->saved_req_buf);
724
725 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
726 hs_ep->ep.name, req_buf, hs_req->req.length);
727
728 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
729 if (!hs_req->req.buf) {
730 hs_req->req.buf = req_buf;
731 dev_err(hsotg->dev,
732 "%s: unable to allocate memory for bounce buffer\n",
733 __func__);
734 return -ENOMEM;
735 }
736
737 /* Save actual buffer */
738 hs_req->saved_req_buf = req_buf;
739
740 if (hs_ep->dir_in)
741 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
742 return 0;
743}
744
1f91b4cc
FB
745static void dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
746 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
7d24c1b5
MYK
747{
748 /* If dma is not being used or buffer was aligned */
749 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
750 return;
751
752 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
753 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
754
755 /* Copy data from bounce buffer on successful out transfer */
756 if (!hs_ep->dir_in && !hs_req->req.status)
757 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
758 hs_req->req.actual);
759
760 /* Free bounce buffer */
761 kfree(hs_req->req.buf);
762
763 hs_req->req.buf = hs_req->saved_req_buf;
764 hs_req->saved_req_buf = NULL;
765}
766
1f91b4cc 767static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
5b7d70c6
BD
768 gfp_t gfp_flags)
769{
1f91b4cc
FB
770 struct dwc2_hsotg_req *hs_req = our_req(req);
771 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 772 struct dwc2_hsotg *hs = hs_ep->parent;
5b7d70c6 773 bool first;
7d24c1b5 774 int ret;
5b7d70c6
BD
775
776 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
777 ep->name, req, req->length, req->buf, req->no_interrupt,
778 req->zero, req->short_not_ok);
779
7ababa92
GH
780 /* Prevent new request submission when controller is suspended */
781 if (hs->lx_state == DWC2_L2) {
782 dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
783 __func__);
784 return -EAGAIN;
785 }
786
5b7d70c6
BD
787 /* initialise status of the request */
788 INIT_LIST_HEAD(&hs_req->queue);
789 req->actual = 0;
790 req->status = -EINPROGRESS;
791
1f91b4cc 792 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
7d24c1b5
MYK
793 if (ret)
794 return ret;
795
5b7d70c6
BD
796 /* if we're using DMA, sync the buffers as necessary */
797 if (using_dma(hs)) {
1f91b4cc 798 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
5b7d70c6
BD
799 if (ret)
800 return ret;
801 }
802
5b7d70c6
BD
803 first = list_empty(&hs_ep->queue);
804 list_add_tail(&hs_req->queue, &hs_ep->queue);
805
806 if (first)
1f91b4cc 807 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
5b7d70c6 808
5b7d70c6
BD
809 return 0;
810}
811
1f91b4cc 812static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
5ad1d316
LM
813 gfp_t gfp_flags)
814{
1f91b4cc 815 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 816 struct dwc2_hsotg *hs = hs_ep->parent;
5ad1d316
LM
817 unsigned long flags = 0;
818 int ret = 0;
819
820 spin_lock_irqsave(&hs->lock, flags);
1f91b4cc 821 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
5ad1d316
LM
822 spin_unlock_irqrestore(&hs->lock, flags);
823
824 return ret;
825}
826
1f91b4cc 827static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
5b7d70c6
BD
828 struct usb_request *req)
829{
1f91b4cc 830 struct dwc2_hsotg_req *hs_req = our_req(req);
5b7d70c6
BD
831
832 kfree(hs_req);
833}
834
835/**
1f91b4cc 836 * dwc2_hsotg_complete_oursetup - setup completion callback
5b7d70c6
BD
837 * @ep: The endpoint the request was on.
838 * @req: The request completed.
839 *
840 * Called on completion of any requests the driver itself
841 * submitted that need cleaning up.
842 */
1f91b4cc 843static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
5b7d70c6
BD
844 struct usb_request *req)
845{
1f91b4cc 846 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 847 struct dwc2_hsotg *hsotg = hs_ep->parent;
5b7d70c6
BD
848
849 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
850
1f91b4cc 851 dwc2_hsotg_ep_free_request(ep, req);
5b7d70c6
BD
852}
853
854/**
855 * ep_from_windex - convert control wIndex value to endpoint
856 * @hsotg: The driver state.
857 * @windex: The control request wIndex field (in host order).
858 *
859 * Convert the given wIndex into a pointer to an driver endpoint
860 * structure, or return NULL if it is not a valid endpoint.
8b9bc460 861 */
1f91b4cc 862static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
863 u32 windex)
864{
1f91b4cc 865 struct dwc2_hsotg_ep *ep;
5b7d70c6
BD
866 int dir = (windex & USB_DIR_IN) ? 1 : 0;
867 int idx = windex & 0x7F;
868
869 if (windex >= 0x100)
870 return NULL;
871
b3f489b2 872 if (idx > hsotg->num_of_eps)
5b7d70c6
BD
873 return NULL;
874
c6f5c050
MYK
875 ep = index_to_ep(hsotg, idx, dir);
876
5b7d70c6
BD
877 if (idx && ep->dir_in != dir)
878 return NULL;
879
880 return ep;
881}
882
9e14d0a5 883/**
1f91b4cc 884 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
9e14d0a5
GH
885 * @hsotg: The driver state.
886 * @testmode: requested usb test mode
887 * Enable usb Test Mode requested by the Host.
888 */
1f91b4cc 889int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
9e14d0a5 890{
95c8bc36 891 int dctl = dwc2_readl(hsotg->regs + DCTL);
9e14d0a5
GH
892
893 dctl &= ~DCTL_TSTCTL_MASK;
894 switch (testmode) {
895 case TEST_J:
896 case TEST_K:
897 case TEST_SE0_NAK:
898 case TEST_PACKET:
899 case TEST_FORCE_EN:
900 dctl |= testmode << DCTL_TSTCTL_SHIFT;
901 break;
902 default:
903 return -EINVAL;
904 }
95c8bc36 905 dwc2_writel(dctl, hsotg->regs + DCTL);
9e14d0a5
GH
906 return 0;
907}
908
5b7d70c6 909/**
1f91b4cc 910 * dwc2_hsotg_send_reply - send reply to control request
5b7d70c6
BD
911 * @hsotg: The device state
912 * @ep: Endpoint 0
913 * @buff: Buffer for request
914 * @length: Length of reply.
915 *
916 * Create a request and queue it on the given endpoint. This is useful as
917 * an internal method of sending replies to certain control requests, etc.
918 */
1f91b4cc
FB
919static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
920 struct dwc2_hsotg_ep *ep,
5b7d70c6
BD
921 void *buff,
922 int length)
923{
924 struct usb_request *req;
925 int ret;
926
927 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
928
1f91b4cc 929 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
5b7d70c6
BD
930 hsotg->ep0_reply = req;
931 if (!req) {
932 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
933 return -ENOMEM;
934 }
935
936 req->buf = hsotg->ep0_buff;
937 req->length = length;
f71b5e25
MYK
938 /*
939 * zero flag is for sending zlp in DATA IN stage. It has no impact on
940 * STATUS stage.
941 */
942 req->zero = 0;
1f91b4cc 943 req->complete = dwc2_hsotg_complete_oursetup;
5b7d70c6
BD
944
945 if (length)
946 memcpy(req->buf, buff, length);
5b7d70c6 947
1f91b4cc 948 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
5b7d70c6
BD
949 if (ret) {
950 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
951 return ret;
952 }
953
954 return 0;
955}
956
957/**
1f91b4cc 958 * dwc2_hsotg_process_req_status - process request GET_STATUS
5b7d70c6
BD
959 * @hsotg: The device state
960 * @ctrl: USB control request
961 */
1f91b4cc 962static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
963 struct usb_ctrlrequest *ctrl)
964{
1f91b4cc
FB
965 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
966 struct dwc2_hsotg_ep *ep;
5b7d70c6
BD
967 __le16 reply;
968 int ret;
969
970 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
971
972 if (!ep0->dir_in) {
973 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
974 return -EINVAL;
975 }
976
977 switch (ctrl->bRequestType & USB_RECIP_MASK) {
978 case USB_RECIP_DEVICE:
979 reply = cpu_to_le16(0); /* bit 0 => self powered,
980 * bit 1 => remote wakeup */
981 break;
982
983 case USB_RECIP_INTERFACE:
984 /* currently, the data result should be zero */
985 reply = cpu_to_le16(0);
986 break;
987
988 case USB_RECIP_ENDPOINT:
989 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
990 if (!ep)
991 return -ENOENT;
992
993 reply = cpu_to_le16(ep->halted ? 1 : 0);
994 break;
995
996 default:
997 return 0;
998 }
999
1000 if (le16_to_cpu(ctrl->wLength) != 2)
1001 return -EINVAL;
1002
1f91b4cc 1003 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
5b7d70c6
BD
1004 if (ret) {
1005 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1006 return ret;
1007 }
1008
1009 return 1;
1010}
1011
51da43b5 1012static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
5b7d70c6 1013
9c39ddc6
AT
1014/**
1015 * get_ep_head - return the first request on the endpoint
1016 * @hs_ep: The controller endpoint to get
1017 *
1018 * Get the first request on the endpoint.
1019 */
1f91b4cc 1020static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
9c39ddc6
AT
1021{
1022 if (list_empty(&hs_ep->queue))
1023 return NULL;
1024
1f91b4cc 1025 return list_first_entry(&hs_ep->queue, struct dwc2_hsotg_req, queue);
9c39ddc6
AT
1026}
1027
5b7d70c6 1028/**
1f91b4cc 1029 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
5b7d70c6
BD
1030 * @hsotg: The device state
1031 * @ctrl: USB control request
1032 */
1f91b4cc 1033static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
1034 struct usb_ctrlrequest *ctrl)
1035{
1f91b4cc
FB
1036 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1037 struct dwc2_hsotg_req *hs_req;
9c39ddc6 1038 bool restart;
5b7d70c6 1039 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1f91b4cc 1040 struct dwc2_hsotg_ep *ep;
26ab3d0c 1041 int ret;
bd9ef7bf 1042 bool halted;
9e14d0a5
GH
1043 u32 recip;
1044 u32 wValue;
1045 u32 wIndex;
5b7d70c6
BD
1046
1047 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1048 __func__, set ? "SET" : "CLEAR");
1049
9e14d0a5
GH
1050 wValue = le16_to_cpu(ctrl->wValue);
1051 wIndex = le16_to_cpu(ctrl->wIndex);
1052 recip = ctrl->bRequestType & USB_RECIP_MASK;
1053
1054 switch (recip) {
1055 case USB_RECIP_DEVICE:
1056 switch (wValue) {
1057 case USB_DEVICE_TEST_MODE:
1058 if ((wIndex & 0xff) != 0)
1059 return -EINVAL;
1060 if (!set)
1061 return -EINVAL;
1062
1063 hsotg->test_mode = wIndex >> 8;
1f91b4cc 1064 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
9e14d0a5
GH
1065 if (ret) {
1066 dev_err(hsotg->dev,
1067 "%s: failed to send reply\n", __func__);
1068 return ret;
1069 }
1070 break;
1071 default:
1072 return -ENOENT;
1073 }
1074 break;
1075
1076 case USB_RECIP_ENDPOINT:
1077 ep = ep_from_windex(hsotg, wIndex);
5b7d70c6
BD
1078 if (!ep) {
1079 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
9e14d0a5 1080 __func__, wIndex);
5b7d70c6
BD
1081 return -ENOENT;
1082 }
1083
9e14d0a5 1084 switch (wValue) {
5b7d70c6 1085 case USB_ENDPOINT_HALT:
bd9ef7bf
RB
1086 halted = ep->halted;
1087
51da43b5 1088 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
26ab3d0c 1089
1f91b4cc 1090 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
26ab3d0c
AT
1091 if (ret) {
1092 dev_err(hsotg->dev,
1093 "%s: failed to send reply\n", __func__);
1094 return ret;
1095 }
9c39ddc6 1096
bd9ef7bf
RB
1097 /*
1098 * we have to complete all requests for ep if it was
1099 * halted, and the halt was cleared by CLEAR_FEATURE
1100 */
1101
1102 if (!set && halted) {
9c39ddc6
AT
1103 /*
1104 * If we have request in progress,
1105 * then complete it
1106 */
1107 if (ep->req) {
1108 hs_req = ep->req;
1109 ep->req = NULL;
1110 list_del_init(&hs_req->queue);
c00dd4a6
GH
1111 if (hs_req->req.complete) {
1112 spin_unlock(&hsotg->lock);
1113 usb_gadget_giveback_request(
1114 &ep->ep, &hs_req->req);
1115 spin_lock(&hsotg->lock);
1116 }
9c39ddc6
AT
1117 }
1118
1119 /* If we have pending request, then start it */
c00dd4a6
GH
1120 if (!ep->req) {
1121 restart = !list_empty(&ep->queue);
1122 if (restart) {
1123 hs_req = get_ep_head(ep);
1f91b4cc 1124 dwc2_hsotg_start_req(hsotg, ep,
c00dd4a6
GH
1125 hs_req, false);
1126 }
9c39ddc6
AT
1127 }
1128 }
1129
5b7d70c6
BD
1130 break;
1131
1132 default:
1133 return -ENOENT;
1134 }
9e14d0a5
GH
1135 break;
1136 default:
1137 return -ENOENT;
1138 }
5b7d70c6
BD
1139 return 1;
1140}
1141
1f91b4cc 1142static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
ab93e014 1143
c9f721b2 1144/**
1f91b4cc 1145 * dwc2_hsotg_stall_ep0 - stall ep0
c9f721b2
RB
1146 * @hsotg: The device state
1147 *
1148 * Set stall for ep0 as response for setup request.
1149 */
1f91b4cc 1150static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
e9ebe7c3 1151{
1f91b4cc 1152 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
c9f721b2
RB
1153 u32 reg;
1154 u32 ctrl;
1155
1156 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1157 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1158
1159 /*
1160 * DxEPCTL_Stall will be cleared by EP once it has
1161 * taken effect, so no need to clear later.
1162 */
1163
95c8bc36 1164 ctrl = dwc2_readl(hsotg->regs + reg);
47a1685f
DN
1165 ctrl |= DXEPCTL_STALL;
1166 ctrl |= DXEPCTL_CNAK;
95c8bc36 1167 dwc2_writel(ctrl, hsotg->regs + reg);
c9f721b2
RB
1168
1169 dev_dbg(hsotg->dev,
47a1685f 1170 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
95c8bc36 1171 ctrl, reg, dwc2_readl(hsotg->regs + reg));
c9f721b2
RB
1172
1173 /*
1174 * complete won't be called, so we enqueue
1175 * setup request here
1176 */
1f91b4cc 1177 dwc2_hsotg_enqueue_setup(hsotg);
c9f721b2
RB
1178}
1179
5b7d70c6 1180/**
1f91b4cc 1181 * dwc2_hsotg_process_control - process a control request
5b7d70c6
BD
1182 * @hsotg: The device state
1183 * @ctrl: The control request received
1184 *
1185 * The controller has received the SETUP phase of a control request, and
1186 * needs to work out what to do next (and whether to pass it on to the
1187 * gadget driver).
1188 */
1f91b4cc 1189static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
5b7d70c6
BD
1190 struct usb_ctrlrequest *ctrl)
1191{
1f91b4cc 1192 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
5b7d70c6
BD
1193 int ret = 0;
1194 u32 dcfg;
1195
e525e743
MYK
1196 dev_dbg(hsotg->dev,
1197 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1198 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1199 ctrl->wIndex, ctrl->wLength);
5b7d70c6 1200
fe0b94ab
MYK
1201 if (ctrl->wLength == 0) {
1202 ep0->dir_in = 1;
1203 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1204 } else if (ctrl->bRequestType & USB_DIR_IN) {
5b7d70c6 1205 ep0->dir_in = 1;
fe0b94ab
MYK
1206 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1207 } else {
1208 ep0->dir_in = 0;
1209 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1210 }
5b7d70c6
BD
1211
1212 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1213 switch (ctrl->bRequest) {
1214 case USB_REQ_SET_ADDRESS:
6d713c15 1215 hsotg->connected = 1;
95c8bc36 1216 dcfg = dwc2_readl(hsotg->regs + DCFG);
47a1685f 1217 dcfg &= ~DCFG_DEVADDR_MASK;
d5dbd3f7
PZ
1218 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1219 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
95c8bc36 1220 dwc2_writel(dcfg, hsotg->regs + DCFG);
5b7d70c6
BD
1221
1222 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1223
1f91b4cc 1224 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
5b7d70c6
BD
1225 return;
1226
1227 case USB_REQ_GET_STATUS:
1f91b4cc 1228 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
5b7d70c6
BD
1229 break;
1230
1231 case USB_REQ_CLEAR_FEATURE:
1232 case USB_REQ_SET_FEATURE:
1f91b4cc 1233 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
5b7d70c6
BD
1234 break;
1235 }
1236 }
1237
1238 /* as a fallback, try delivering it to the driver to deal with */
1239
1240 if (ret == 0 && hsotg->driver) {
93f599f2 1241 spin_unlock(&hsotg->lock);
5b7d70c6 1242 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
93f599f2 1243 spin_lock(&hsotg->lock);
5b7d70c6
BD
1244 if (ret < 0)
1245 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1246 }
1247
8b9bc460
LM
1248 /*
1249 * the request is either unhandlable, or is not formatted correctly
5b7d70c6
BD
1250 * so respond with a STALL for the status stage to indicate failure.
1251 */
1252
c9f721b2 1253 if (ret < 0)
1f91b4cc 1254 dwc2_hsotg_stall_ep0(hsotg);
5b7d70c6
BD
1255}
1256
5b7d70c6 1257/**
1f91b4cc 1258 * dwc2_hsotg_complete_setup - completion of a setup transfer
5b7d70c6
BD
1259 * @ep: The endpoint the request was on.
1260 * @req: The request completed.
1261 *
1262 * Called on completion of any requests the driver itself submitted for
1263 * EP0 setup packets
1264 */
1f91b4cc 1265static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
5b7d70c6
BD
1266 struct usb_request *req)
1267{
1f91b4cc 1268 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 1269 struct dwc2_hsotg *hsotg = hs_ep->parent;
5b7d70c6
BD
1270
1271 if (req->status < 0) {
1272 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1273 return;
1274 }
1275
93f599f2 1276 spin_lock(&hsotg->lock);
5b7d70c6 1277 if (req->actual == 0)
1f91b4cc 1278 dwc2_hsotg_enqueue_setup(hsotg);
5b7d70c6 1279 else
1f91b4cc 1280 dwc2_hsotg_process_control(hsotg, req->buf);
93f599f2 1281 spin_unlock(&hsotg->lock);
5b7d70c6
BD
1282}
1283
1284/**
1f91b4cc 1285 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
5b7d70c6
BD
1286 * @hsotg: The device state.
1287 *
1288 * Enqueue a request on EP0 if necessary to received any SETUP packets
1289 * received from the host.
1290 */
1f91b4cc 1291static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
5b7d70c6
BD
1292{
1293 struct usb_request *req = hsotg->ctrl_req;
1f91b4cc 1294 struct dwc2_hsotg_req *hs_req = our_req(req);
5b7d70c6
BD
1295 int ret;
1296
1297 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1298
1299 req->zero = 0;
1300 req->length = 8;
1301 req->buf = hsotg->ctrl_buff;
1f91b4cc 1302 req->complete = dwc2_hsotg_complete_setup;
5b7d70c6
BD
1303
1304 if (!list_empty(&hs_req->queue)) {
1305 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1306 return;
1307 }
1308
c6f5c050 1309 hsotg->eps_out[0]->dir_in = 0;
8a20fa45 1310 hsotg->eps_out[0]->send_zlp = 0;
fe0b94ab 1311 hsotg->ep0_state = DWC2_EP0_SETUP;
5b7d70c6 1312
1f91b4cc 1313 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
5b7d70c6
BD
1314 if (ret < 0) {
1315 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
8b9bc460
LM
1316 /*
1317 * Don't think there's much we can do other than watch the
1318 * driver fail.
1319 */
5b7d70c6
BD
1320 }
1321}
1322
1f91b4cc
FB
1323static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
1324 struct dwc2_hsotg_ep *hs_ep)
fe0b94ab
MYK
1325{
1326 u32 ctrl;
1327 u8 index = hs_ep->index;
1328 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1329 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1330
ccb34a91
MYK
1331 if (hs_ep->dir_in)
1332 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
1333 index);
1334 else
1335 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
1336 index);
fe0b94ab 1337
95c8bc36
AS
1338 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1339 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1340 epsiz_reg);
fe0b94ab 1341
95c8bc36 1342 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
fe0b94ab
MYK
1343 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1344 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1345 ctrl |= DXEPCTL_USBACTEP;
95c8bc36 1346 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
fe0b94ab
MYK
1347}
1348
5b7d70c6 1349/**
1f91b4cc 1350 * dwc2_hsotg_complete_request - complete a request given to us
5b7d70c6
BD
1351 * @hsotg: The device state.
1352 * @hs_ep: The endpoint the request was on.
1353 * @hs_req: The request to complete.
1354 * @result: The result code (0 => Ok, otherwise errno)
1355 *
1356 * The given request has finished, so call the necessary completion
1357 * if it has one and then look to see if we can start a new request
1358 * on the endpoint.
1359 *
1360 * Note, expects the ep to already be locked as appropriate.
8b9bc460 1361 */
1f91b4cc
FB
1362static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
1363 struct dwc2_hsotg_ep *hs_ep,
1364 struct dwc2_hsotg_req *hs_req,
5b7d70c6
BD
1365 int result)
1366{
1367 bool restart;
1368
1369 if (!hs_req) {
1370 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1371 return;
1372 }
1373
1374 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1375 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1376
8b9bc460
LM
1377 /*
1378 * only replace the status if we've not already set an error
1379 * from a previous transaction
1380 */
5b7d70c6
BD
1381
1382 if (hs_req->req.status == -EINPROGRESS)
1383 hs_req->req.status = result;
1384
44583fec
YL
1385 if (using_dma(hsotg))
1386 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1387
1f91b4cc 1388 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
7d24c1b5 1389
5b7d70c6
BD
1390 hs_ep->req = NULL;
1391 list_del_init(&hs_req->queue);
1392
8b9bc460
LM
1393 /*
1394 * call the complete request with the locks off, just in case the
1395 * request tries to queue more work for this endpoint.
1396 */
5b7d70c6
BD
1397
1398 if (hs_req->req.complete) {
22258f49 1399 spin_unlock(&hsotg->lock);
304f7e5e 1400 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
22258f49 1401 spin_lock(&hsotg->lock);
5b7d70c6
BD
1402 }
1403
8b9bc460
LM
1404 /*
1405 * Look to see if there is anything else to do. Note, the completion
5b7d70c6 1406 * of the previous request may have caused a new request to be started
8b9bc460
LM
1407 * so be careful when doing this.
1408 */
5b7d70c6
BD
1409
1410 if (!hs_ep->req && result >= 0) {
1411 restart = !list_empty(&hs_ep->queue);
1412 if (restart) {
1413 hs_req = get_ep_head(hs_ep);
1f91b4cc 1414 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
5b7d70c6
BD
1415 }
1416 }
1417}
1418
5b7d70c6 1419/**
1f91b4cc 1420 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
5b7d70c6
BD
1421 * @hsotg: The device state.
1422 * @ep_idx: The endpoint index for the data
1423 * @size: The size of data in the fifo, in bytes
1424 *
1425 * The FIFO status shows there is data to read from the FIFO for a given
1426 * endpoint, so sort out whether we need to read the data into a request
1427 * that has been made for that endpoint.
1428 */
1f91b4cc 1429static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
5b7d70c6 1430{
1f91b4cc
FB
1431 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
1432 struct dwc2_hsotg_req *hs_req = hs_ep->req;
94cb8fd6 1433 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
5b7d70c6
BD
1434 int to_read;
1435 int max_req;
1436 int read_ptr;
1437
22258f49 1438
5b7d70c6 1439 if (!hs_req) {
95c8bc36 1440 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
5b7d70c6
BD
1441 int ptr;
1442
6b448af4 1443 dev_dbg(hsotg->dev,
47a1685f 1444 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
5b7d70c6
BD
1445 __func__, size, ep_idx, epctl);
1446
1447 /* dump the data from the FIFO, we've nothing we can do */
1448 for (ptr = 0; ptr < size; ptr += 4)
95c8bc36 1449 (void)dwc2_readl(fifo);
5b7d70c6
BD
1450
1451 return;
1452 }
1453
5b7d70c6
BD
1454 to_read = size;
1455 read_ptr = hs_req->req.actual;
1456 max_req = hs_req->req.length - read_ptr;
1457
a33e7136
BD
1458 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1459 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1460
5b7d70c6 1461 if (to_read > max_req) {
8b9bc460
LM
1462 /*
1463 * more data appeared than we where willing
5b7d70c6
BD
1464 * to deal with in this request.
1465 */
1466
1467 /* currently we don't deal this */
1468 WARN_ON_ONCE(1);
1469 }
1470
5b7d70c6
BD
1471 hs_ep->total_data += to_read;
1472 hs_req->req.actual += to_read;
1473 to_read = DIV_ROUND_UP(to_read, 4);
1474
8b9bc460
LM
1475 /*
1476 * note, we might over-write the buffer end by 3 bytes depending on
1477 * alignment of the data.
1478 */
1a7ed5be 1479 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
5b7d70c6
BD
1480}
1481
1482/**
1f91b4cc 1483 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
5b7d70c6 1484 * @hsotg: The device instance
fe0b94ab 1485 * @dir_in: If IN zlp
5b7d70c6
BD
1486 *
1487 * Generate a zero-length IN packet request for terminating a SETUP
1488 * transaction.
1489 *
1490 * Note, since we don't write any data to the TxFIFO, then it is
25985edc 1491 * currently believed that we do not need to wait for any space in
5b7d70c6
BD
1492 * the TxFIFO.
1493 */
1f91b4cc 1494static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
5b7d70c6 1495{
c6f5c050 1496 /* eps_out[0] is used in both directions */
fe0b94ab
MYK
1497 hsotg->eps_out[0]->dir_in = dir_in;
1498 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
5b7d70c6 1499
1f91b4cc 1500 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
5b7d70c6
BD
1501}
1502
ec1f9d9f
RB
1503static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
1504 u32 epctl_reg)
1505{
1506 u32 ctrl;
1507
1508 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
1509 if (ctrl & DXEPCTL_EOFRNUM)
1510 ctrl |= DXEPCTL_SETEVENFR;
1511 else
1512 ctrl |= DXEPCTL_SETODDFR;
1513 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
1514}
1515
5b7d70c6 1516/**
1f91b4cc 1517 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
5b7d70c6
BD
1518 * @hsotg: The device instance
1519 * @epnum: The endpoint received from
5b7d70c6
BD
1520 *
1521 * The RXFIFO has delivered an OutDone event, which means that the data
1522 * transfer for an OUT endpoint has been completed, either by a short
1523 * packet or by the finish of a transfer.
8b9bc460 1524 */
1f91b4cc 1525static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
5b7d70c6 1526{
95c8bc36 1527 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
1f91b4cc
FB
1528 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
1529 struct dwc2_hsotg_req *hs_req = hs_ep->req;
5b7d70c6 1530 struct usb_request *req = &hs_req->req;
47a1685f 1531 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
5b7d70c6
BD
1532 int result = 0;
1533
1534 if (!hs_req) {
1535 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1536 return;
1537 }
1538
fe0b94ab
MYK
1539 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
1540 dev_dbg(hsotg->dev, "zlp packet received\n");
1f91b4cc
FB
1541 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1542 dwc2_hsotg_enqueue_setup(hsotg);
fe0b94ab
MYK
1543 return;
1544 }
1545
5b7d70c6 1546 if (using_dma(hsotg)) {
5b7d70c6 1547 unsigned size_done;
5b7d70c6 1548
8b9bc460
LM
1549 /*
1550 * Calculate the size of the transfer by checking how much
5b7d70c6
BD
1551 * is left in the endpoint size register and then working it
1552 * out from the amount we loaded for the transfer.
1553 *
1554 * We need to do this as DMA pointers are always 32bit aligned
1555 * so may overshoot/undershoot the transfer.
1556 */
1557
5b7d70c6
BD
1558 size_done = hs_ep->size_loaded - size_left;
1559 size_done += hs_ep->last_load;
1560
1561 req->actual = size_done;
1562 }
1563
a33e7136
BD
1564 /* if there is more request to do, schedule new transfer */
1565 if (req->actual < req->length && size_left == 0) {
1f91b4cc 1566 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
a33e7136
BD
1567 return;
1568 }
1569
5b7d70c6
BD
1570 if (req->actual < req->length && req->short_not_ok) {
1571 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1572 __func__, req->actual, req->length);
1573
8b9bc460
LM
1574 /*
1575 * todo - what should we return here? there's no one else
1576 * even bothering to check the status.
1577 */
5b7d70c6
BD
1578 }
1579
fe0b94ab
MYK
1580 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
1581 /* Move to STATUS IN */
1f91b4cc 1582 dwc2_hsotg_ep0_zlp(hsotg, true);
fe0b94ab 1583 return;
5b7d70c6
BD
1584 }
1585
ec1f9d9f
RB
1586 /*
1587 * Slave mode OUT transfers do not go through XferComplete so
1588 * adjust the ISOC parity here.
1589 */
1590 if (!using_dma(hsotg)) {
1591 hs_ep->has_correct_parity = 1;
1592 if (hs_ep->isochronous && hs_ep->interval == 1)
1593 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
1594 }
1595
1f91b4cc 1596 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
5b7d70c6
BD
1597}
1598
1599/**
1f91b4cc 1600 * dwc2_hsotg_read_frameno - read current frame number
5b7d70c6
BD
1601 * @hsotg: The device instance
1602 *
1603 * Return the current frame number
8b9bc460 1604 */
1f91b4cc 1605static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
5b7d70c6
BD
1606{
1607 u32 dsts;
1608
95c8bc36 1609 dsts = dwc2_readl(hsotg->regs + DSTS);
94cb8fd6
LM
1610 dsts &= DSTS_SOFFN_MASK;
1611 dsts >>= DSTS_SOFFN_SHIFT;
5b7d70c6
BD
1612
1613 return dsts;
1614}
1615
1616/**
1f91b4cc 1617 * dwc2_hsotg_handle_rx - RX FIFO has data
5b7d70c6
BD
1618 * @hsotg: The device instance
1619 *
1620 * The IRQ handler has detected that the RX FIFO has some data in it
1621 * that requires processing, so find out what is in there and do the
1622 * appropriate read.
1623 *
25985edc 1624 * The RXFIFO is a true FIFO, the packets coming out are still in packet
5b7d70c6
BD
1625 * chunks, so if you have x packets received on an endpoint you'll get x
1626 * FIFO events delivered, each with a packet's worth of data in it.
1627 *
1628 * When using DMA, we should not be processing events from the RXFIFO
1629 * as the actual data should be sent to the memory directly and we turn
1630 * on the completion interrupts to get notifications of transfer completion.
1631 */
1f91b4cc 1632static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
5b7d70c6 1633{
95c8bc36 1634 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
5b7d70c6
BD
1635 u32 epnum, status, size;
1636
1637 WARN_ON(using_dma(hsotg));
1638
47a1685f
DN
1639 epnum = grxstsr & GRXSTS_EPNUM_MASK;
1640 status = grxstsr & GRXSTS_PKTSTS_MASK;
5b7d70c6 1641
47a1685f
DN
1642 size = grxstsr & GRXSTS_BYTECNT_MASK;
1643 size >>= GRXSTS_BYTECNT_SHIFT;
5b7d70c6 1644
d7c747c5 1645 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
5b7d70c6
BD
1646 __func__, grxstsr, size, epnum);
1647
47a1685f
DN
1648 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
1649 case GRXSTS_PKTSTS_GLOBALOUTNAK:
1650 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
5b7d70c6
BD
1651 break;
1652
47a1685f 1653 case GRXSTS_PKTSTS_OUTDONE:
5b7d70c6 1654 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1f91b4cc 1655 dwc2_hsotg_read_frameno(hsotg));
5b7d70c6
BD
1656
1657 if (!using_dma(hsotg))
1f91b4cc 1658 dwc2_hsotg_handle_outdone(hsotg, epnum);
5b7d70c6
BD
1659 break;
1660
47a1685f 1661 case GRXSTS_PKTSTS_SETUPDONE:
5b7d70c6
BD
1662 dev_dbg(hsotg->dev,
1663 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1f91b4cc 1664 dwc2_hsotg_read_frameno(hsotg),
95c8bc36 1665 dwc2_readl(hsotg->regs + DOEPCTL(0)));
fe0b94ab 1666 /*
1f91b4cc 1667 * Call dwc2_hsotg_handle_outdone here if it was not called from
fe0b94ab
MYK
1668 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
1669 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
1670 */
1671 if (hsotg->ep0_state == DWC2_EP0_SETUP)
1f91b4cc 1672 dwc2_hsotg_handle_outdone(hsotg, epnum);
5b7d70c6
BD
1673 break;
1674
47a1685f 1675 case GRXSTS_PKTSTS_OUTRX:
1f91b4cc 1676 dwc2_hsotg_rx_data(hsotg, epnum, size);
5b7d70c6
BD
1677 break;
1678
47a1685f 1679 case GRXSTS_PKTSTS_SETUPRX:
5b7d70c6
BD
1680 dev_dbg(hsotg->dev,
1681 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1f91b4cc 1682 dwc2_hsotg_read_frameno(hsotg),
95c8bc36 1683 dwc2_readl(hsotg->regs + DOEPCTL(0)));
5b7d70c6 1684
fe0b94ab
MYK
1685 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
1686
1f91b4cc 1687 dwc2_hsotg_rx_data(hsotg, epnum, size);
5b7d70c6
BD
1688 break;
1689
1690 default:
1691 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1692 __func__, grxstsr);
1693
1f91b4cc 1694 dwc2_hsotg_dump(hsotg);
5b7d70c6
BD
1695 break;
1696 }
1697}
1698
1699/**
1f91b4cc 1700 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
5b7d70c6 1701 * @mps: The maximum packet size in bytes.
8b9bc460 1702 */
1f91b4cc 1703static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
5b7d70c6
BD
1704{
1705 switch (mps) {
1706 case 64:
94cb8fd6 1707 return D0EPCTL_MPS_64;
5b7d70c6 1708 case 32:
94cb8fd6 1709 return D0EPCTL_MPS_32;
5b7d70c6 1710 case 16:
94cb8fd6 1711 return D0EPCTL_MPS_16;
5b7d70c6 1712 case 8:
94cb8fd6 1713 return D0EPCTL_MPS_8;
5b7d70c6
BD
1714 }
1715
1716 /* bad max packet size, warn and return invalid result */
1717 WARN_ON(1);
1718 return (u32)-1;
1719}
1720
1721/**
1f91b4cc 1722 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
5b7d70c6
BD
1723 * @hsotg: The driver state.
1724 * @ep: The index number of the endpoint
1725 * @mps: The maximum packet size in bytes
1726 *
1727 * Configure the maximum packet size for the given endpoint, updating
1728 * the hardware control registers to reflect this.
1729 */
1f91b4cc 1730static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
c6f5c050 1731 unsigned int ep, unsigned int mps, unsigned int dir_in)
5b7d70c6 1732{
1f91b4cc 1733 struct dwc2_hsotg_ep *hs_ep;
5b7d70c6
BD
1734 void __iomem *regs = hsotg->regs;
1735 u32 mpsval;
4fca54aa 1736 u32 mcval;
5b7d70c6
BD
1737 u32 reg;
1738
c6f5c050
MYK
1739 hs_ep = index_to_ep(hsotg, ep, dir_in);
1740 if (!hs_ep)
1741 return;
1742
5b7d70c6
BD
1743 if (ep == 0) {
1744 /* EP0 is a special case */
1f91b4cc 1745 mpsval = dwc2_hsotg_ep0_mps(mps);
5b7d70c6
BD
1746 if (mpsval > 3)
1747 goto bad_mps;
e9edd199 1748 hs_ep->ep.maxpacket = mps;
4fca54aa 1749 hs_ep->mc = 1;
5b7d70c6 1750 } else {
47a1685f 1751 mpsval = mps & DXEPCTL_MPS_MASK;
e9edd199 1752 if (mpsval > 1024)
5b7d70c6 1753 goto bad_mps;
4fca54aa
RB
1754 mcval = ((mps >> 11) & 0x3) + 1;
1755 hs_ep->mc = mcval;
1756 if (mcval > 3)
1757 goto bad_mps;
e9edd199 1758 hs_ep->ep.maxpacket = mpsval;
5b7d70c6
BD
1759 }
1760
c6f5c050 1761 if (dir_in) {
95c8bc36 1762 reg = dwc2_readl(regs + DIEPCTL(ep));
c6f5c050
MYK
1763 reg &= ~DXEPCTL_MPS_MASK;
1764 reg |= mpsval;
95c8bc36 1765 dwc2_writel(reg, regs + DIEPCTL(ep));
c6f5c050 1766 } else {
95c8bc36 1767 reg = dwc2_readl(regs + DOEPCTL(ep));
47a1685f 1768 reg &= ~DXEPCTL_MPS_MASK;
659ad60c 1769 reg |= mpsval;
95c8bc36 1770 dwc2_writel(reg, regs + DOEPCTL(ep));
659ad60c 1771 }
5b7d70c6
BD
1772
1773 return;
1774
1775bad_mps:
1776 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1777}
1778
9c39ddc6 1779/**
1f91b4cc 1780 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
9c39ddc6
AT
1781 * @hsotg: The driver state
1782 * @idx: The index for the endpoint (0..15)
1783 */
1f91b4cc 1784static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
9c39ddc6
AT
1785{
1786 int timeout;
1787 int val;
1788
95c8bc36
AS
1789 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
1790 hsotg->regs + GRSTCTL);
9c39ddc6
AT
1791
1792 /* wait until the fifo is flushed */
1793 timeout = 100;
1794
1795 while (1) {
95c8bc36 1796 val = dwc2_readl(hsotg->regs + GRSTCTL);
9c39ddc6 1797
47a1685f 1798 if ((val & (GRSTCTL_TXFFLSH)) == 0)
9c39ddc6
AT
1799 break;
1800
1801 if (--timeout == 0) {
1802 dev_err(hsotg->dev,
1803 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1804 __func__, val);
e0cbe595 1805 break;
9c39ddc6
AT
1806 }
1807
1808 udelay(1);
1809 }
1810}
5b7d70c6
BD
1811
1812/**
1f91b4cc 1813 * dwc2_hsotg_trytx - check to see if anything needs transmitting
5b7d70c6
BD
1814 * @hsotg: The driver state
1815 * @hs_ep: The driver endpoint to check.
1816 *
1817 * Check to see if there is a request that has data to send, and if so
1818 * make an attempt to write data into the FIFO.
1819 */
1f91b4cc
FB
1820static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
1821 struct dwc2_hsotg_ep *hs_ep)
5b7d70c6 1822{
1f91b4cc 1823 struct dwc2_hsotg_req *hs_req = hs_ep->req;
5b7d70c6 1824
afcf4169
RB
1825 if (!hs_ep->dir_in || !hs_req) {
1826 /**
1827 * if request is not enqueued, we disable interrupts
1828 * for endpoints, excepting ep0
1829 */
1830 if (hs_ep->index != 0)
1f91b4cc 1831 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
afcf4169 1832 hs_ep->dir_in, 0);
5b7d70c6 1833 return 0;
afcf4169 1834 }
5b7d70c6
BD
1835
1836 if (hs_req->req.actual < hs_req->req.length) {
1837 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1838 hs_ep->index);
1f91b4cc 1839 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
5b7d70c6
BD
1840 }
1841
1842 return 0;
1843}
1844
1845/**
1f91b4cc 1846 * dwc2_hsotg_complete_in - complete IN transfer
5b7d70c6
BD
1847 * @hsotg: The device state.
1848 * @hs_ep: The endpoint that has just completed.
1849 *
1850 * An IN transfer has been completed, update the transfer's state and then
1851 * call the relevant completion routines.
1852 */
1f91b4cc
FB
1853static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
1854 struct dwc2_hsotg_ep *hs_ep)
5b7d70c6 1855{
1f91b4cc 1856 struct dwc2_hsotg_req *hs_req = hs_ep->req;
95c8bc36 1857 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
5b7d70c6
BD
1858 int size_left, size_done;
1859
1860 if (!hs_req) {
1861 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1862 return;
1863 }
1864
d3ca0259 1865 /* Finish ZLP handling for IN EP0 transactions */
fe0b94ab
MYK
1866 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
1867 dev_dbg(hsotg->dev, "zlp packet sent\n");
1f91b4cc 1868 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
9e14d0a5
GH
1869 if (hsotg->test_mode) {
1870 int ret;
1871
1f91b4cc 1872 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
9e14d0a5
GH
1873 if (ret < 0) {
1874 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
1875 hsotg->test_mode);
1f91b4cc 1876 dwc2_hsotg_stall_ep0(hsotg);
9e14d0a5
GH
1877 return;
1878 }
1879 }
1f91b4cc 1880 dwc2_hsotg_enqueue_setup(hsotg);
d3ca0259
LM
1881 return;
1882 }
1883
8b9bc460
LM
1884 /*
1885 * Calculate the size of the transfer by checking how much is left
5b7d70c6
BD
1886 * in the endpoint size register and then working it out from
1887 * the amount we loaded for the transfer.
1888 *
1889 * We do this even for DMA, as the transfer may have incremented
1890 * past the end of the buffer (DMA transfers are always 32bit
1891 * aligned).
1892 */
1893
47a1685f 1894 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
5b7d70c6
BD
1895
1896 size_done = hs_ep->size_loaded - size_left;
1897 size_done += hs_ep->last_load;
1898
1899 if (hs_req->req.actual != size_done)
1900 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1901 __func__, hs_req->req.actual, size_done);
1902
1903 hs_req->req.actual = size_done;
d3ca0259
LM
1904 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1905 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
1906
5b7d70c6
BD
1907 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1908 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1f91b4cc 1909 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
fe0b94ab
MYK
1910 return;
1911 }
1912
f71b5e25 1913 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
8a20fa45 1914 if (hs_ep->send_zlp) {
1f91b4cc 1915 dwc2_hsotg_program_zlp(hsotg, hs_ep);
8a20fa45 1916 hs_ep->send_zlp = 0;
f71b5e25
MYK
1917 /* transfer will be completed on next complete interrupt */
1918 return;
1919 }
1920
fe0b94ab
MYK
1921 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
1922 /* Move to STATUS OUT */
1f91b4cc 1923 dwc2_hsotg_ep0_zlp(hsotg, false);
fe0b94ab
MYK
1924 return;
1925 }
1926
1f91b4cc 1927 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
5b7d70c6
BD
1928}
1929
1930/**
1f91b4cc 1931 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
5b7d70c6
BD
1932 * @hsotg: The driver state
1933 * @idx: The index for the endpoint (0..15)
1934 * @dir_in: Set if this is an IN endpoint
1935 *
1936 * Process and clear any interrupt pending for an individual endpoint
8b9bc460 1937 */
1f91b4cc 1938static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
5b7d70c6
BD
1939 int dir_in)
1940{
1f91b4cc 1941 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
94cb8fd6
LM
1942 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1943 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1944 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
5b7d70c6 1945 u32 ints;
1479e841 1946 u32 ctrl;
5b7d70c6 1947
95c8bc36
AS
1948 ints = dwc2_readl(hsotg->regs + epint_reg);
1949 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
5b7d70c6 1950
a3395f0d 1951 /* Clear endpoint interrupts */
95c8bc36 1952 dwc2_writel(ints, hsotg->regs + epint_reg);
a3395f0d 1953
c6f5c050
MYK
1954 if (!hs_ep) {
1955 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
1956 __func__, idx, dir_in ? "in" : "out");
1957 return;
1958 }
1959
5b7d70c6
BD
1960 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1961 __func__, idx, dir_in ? "in" : "out", ints);
1962
b787d755
MYK
1963 /* Don't process XferCompl interrupt if it is a setup packet */
1964 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
1965 ints &= ~DXEPINT_XFERCOMPL;
1966
47a1685f 1967 if (ints & DXEPINT_XFERCOMPL) {
ec1f9d9f
RB
1968 hs_ep->has_correct_parity = 1;
1969 if (hs_ep->isochronous && hs_ep->interval == 1)
1970 dwc2_hsotg_change_ep_iso_parity(hsotg, epctl_reg);
1479e841 1971
5b7d70c6 1972 dev_dbg(hsotg->dev,
47a1685f 1973 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
95c8bc36
AS
1974 __func__, dwc2_readl(hsotg->regs + epctl_reg),
1975 dwc2_readl(hsotg->regs + epsiz_reg));
5b7d70c6 1976
8b9bc460
LM
1977 /*
1978 * we get OutDone from the FIFO, so we only need to look
1979 * at completing IN requests here
1980 */
5b7d70c6 1981 if (dir_in) {
1f91b4cc 1982 dwc2_hsotg_complete_in(hsotg, hs_ep);
5b7d70c6 1983
c9a64ea8 1984 if (idx == 0 && !hs_ep->req)
1f91b4cc 1985 dwc2_hsotg_enqueue_setup(hsotg);
5b7d70c6 1986 } else if (using_dma(hsotg)) {
8b9bc460
LM
1987 /*
1988 * We're using DMA, we need to fire an OutDone here
1989 * as we ignore the RXFIFO.
1990 */
5b7d70c6 1991
1f91b4cc 1992 dwc2_hsotg_handle_outdone(hsotg, idx);
5b7d70c6 1993 }
5b7d70c6
BD
1994 }
1995
47a1685f 1996 if (ints & DXEPINT_EPDISBLD) {
5b7d70c6 1997 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
5b7d70c6 1998
9c39ddc6 1999 if (dir_in) {
95c8bc36 2000 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
9c39ddc6 2001
1f91b4cc 2002 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
9c39ddc6 2003
47a1685f
DN
2004 if ((epctl & DXEPCTL_STALL) &&
2005 (epctl & DXEPCTL_EPTYPE_BULK)) {
95c8bc36 2006 int dctl = dwc2_readl(hsotg->regs + DCTL);
9c39ddc6 2007
47a1685f 2008 dctl |= DCTL_CGNPINNAK;
95c8bc36 2009 dwc2_writel(dctl, hsotg->regs + DCTL);
9c39ddc6
AT
2010 }
2011 }
2012 }
2013
47a1685f 2014 if (ints & DXEPINT_AHBERR)
5b7d70c6 2015 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
5b7d70c6 2016
47a1685f 2017 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
5b7d70c6
BD
2018 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2019
2020 if (using_dma(hsotg) && idx == 0) {
8b9bc460
LM
2021 /*
2022 * this is the notification we've received a
5b7d70c6
BD
2023 * setup packet. In non-DMA mode we'd get this
2024 * from the RXFIFO, instead we need to process
8b9bc460
LM
2025 * the setup here.
2026 */
5b7d70c6
BD
2027
2028 if (dir_in)
2029 WARN_ON_ONCE(1);
2030 else
1f91b4cc 2031 dwc2_hsotg_handle_outdone(hsotg, 0);
5b7d70c6 2032 }
5b7d70c6
BD
2033 }
2034
47a1685f 2035 if (ints & DXEPINT_BACK2BACKSETUP)
5b7d70c6 2036 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
5b7d70c6 2037
1479e841 2038 if (dir_in && !hs_ep->isochronous) {
8b9bc460 2039 /* not sure if this is important, but we'll clear it anyway */
47a1685f 2040 if (ints & DIEPMSK_INTKNTXFEMPMSK) {
5b7d70c6
BD
2041 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2042 __func__, idx);
5b7d70c6
BD
2043 }
2044
2045 /* this probably means something bad is happening */
47a1685f 2046 if (ints & DIEPMSK_INTKNEPMISMSK) {
5b7d70c6
BD
2047 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2048 __func__, idx);
5b7d70c6 2049 }
10aebc77
BD
2050
2051 /* FIFO has space or is empty (see GAHBCFG) */
2052 if (hsotg->dedicated_fifos &&
47a1685f 2053 ints & DIEPMSK_TXFIFOEMPTY) {
10aebc77
BD
2054 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2055 __func__, idx);
70fa030f 2056 if (!using_dma(hsotg))
1f91b4cc 2057 dwc2_hsotg_trytx(hsotg, hs_ep);
10aebc77 2058 }
5b7d70c6 2059 }
5b7d70c6
BD
2060}
2061
2062/**
1f91b4cc 2063 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
5b7d70c6
BD
2064 * @hsotg: The device state.
2065 *
2066 * Handle updating the device settings after the enumeration phase has
2067 * been completed.
8b9bc460 2068 */
1f91b4cc 2069static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
5b7d70c6 2070{
95c8bc36 2071 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
9b2667f1 2072 int ep0_mps = 0, ep_mps = 8;
5b7d70c6 2073
8b9bc460
LM
2074 /*
2075 * This should signal the finish of the enumeration phase
5b7d70c6 2076 * of the USB handshaking, so we should now know what rate
8b9bc460
LM
2077 * we connected at.
2078 */
5b7d70c6
BD
2079
2080 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2081
8b9bc460
LM
2082 /*
2083 * note, since we're limited by the size of transfer on EP0, and
5b7d70c6 2084 * it seems IN transfers must be a even number of packets we do
8b9bc460
LM
2085 * not advertise a 64byte MPS on EP0.
2086 */
5b7d70c6
BD
2087
2088 /* catch both EnumSpd_FS and EnumSpd_FS48 */
6d76c92c 2089 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
47a1685f
DN
2090 case DSTS_ENUMSPD_FS:
2091 case DSTS_ENUMSPD_FS48:
5b7d70c6 2092 hsotg->gadget.speed = USB_SPEED_FULL;
5b7d70c6 2093 ep0_mps = EP0_MPS_LIMIT;
295538ff 2094 ep_mps = 1023;
5b7d70c6
BD
2095 break;
2096
47a1685f 2097 case DSTS_ENUMSPD_HS:
5b7d70c6 2098 hsotg->gadget.speed = USB_SPEED_HIGH;
5b7d70c6 2099 ep0_mps = EP0_MPS_LIMIT;
295538ff 2100 ep_mps = 1024;
5b7d70c6
BD
2101 break;
2102
47a1685f 2103 case DSTS_ENUMSPD_LS:
5b7d70c6 2104 hsotg->gadget.speed = USB_SPEED_LOW;
8b9bc460
LM
2105 /*
2106 * note, we don't actually support LS in this driver at the
5b7d70c6
BD
2107 * moment, and the documentation seems to imply that it isn't
2108 * supported by the PHYs on some of the devices.
2109 */
2110 break;
2111 }
e538dfda
MN
2112 dev_info(hsotg->dev, "new device is %s\n",
2113 usb_speed_string(hsotg->gadget.speed));
5b7d70c6 2114
8b9bc460
LM
2115 /*
2116 * we should now know the maximum packet size for an
2117 * endpoint, so set the endpoints to a default value.
2118 */
5b7d70c6
BD
2119
2120 if (ep0_mps) {
2121 int i;
c6f5c050 2122 /* Initialize ep0 for both in and out directions */
1f91b4cc
FB
2123 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 1);
2124 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0);
c6f5c050
MYK
2125 for (i = 1; i < hsotg->num_of_eps; i++) {
2126 if (hsotg->eps_in[i])
1f91b4cc 2127 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 1);
c6f5c050 2128 if (hsotg->eps_out[i])
1f91b4cc 2129 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 0);
c6f5c050 2130 }
5b7d70c6
BD
2131 }
2132
2133 /* ensure after enumeration our EP0 is active */
2134
1f91b4cc 2135 dwc2_hsotg_enqueue_setup(hsotg);
5b7d70c6
BD
2136
2137 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
95c8bc36
AS
2138 dwc2_readl(hsotg->regs + DIEPCTL0),
2139 dwc2_readl(hsotg->regs + DOEPCTL0));
5b7d70c6
BD
2140}
2141
2142/**
2143 * kill_all_requests - remove all requests from the endpoint's queue
2144 * @hsotg: The device state.
2145 * @ep: The endpoint the requests may be on.
2146 * @result: The result code to use.
5b7d70c6
BD
2147 *
2148 * Go through the requests on the given endpoint and mark them
2149 * completed with the given result code.
2150 */
941fcce4 2151static void kill_all_requests(struct dwc2_hsotg *hsotg,
1f91b4cc 2152 struct dwc2_hsotg_ep *ep,
6b448af4 2153 int result)
5b7d70c6 2154{
1f91b4cc 2155 struct dwc2_hsotg_req *req, *treq;
b203d0a2 2156 unsigned size;
5b7d70c6 2157
6b448af4 2158 ep->req = NULL;
5b7d70c6 2159
6b448af4 2160 list_for_each_entry_safe(req, treq, &ep->queue, queue)
1f91b4cc 2161 dwc2_hsotg_complete_request(hsotg, ep, req,
5b7d70c6 2162 result);
6b448af4 2163
b203d0a2
RB
2164 if (!hsotg->dedicated_fifos)
2165 return;
95c8bc36 2166 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4;
b203d0a2 2167 if (size < ep->fifo_size)
1f91b4cc 2168 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
5b7d70c6
BD
2169}
2170
5b7d70c6 2171/**
1f91b4cc 2172 * dwc2_hsotg_disconnect - disconnect service
5b7d70c6
BD
2173 * @hsotg: The device state.
2174 *
5e891342
LM
2175 * The device has been disconnected. Remove all current
2176 * transactions and signal the gadget driver that this
2177 * has happened.
8b9bc460 2178 */
1f91b4cc 2179void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
5b7d70c6
BD
2180{
2181 unsigned ep;
2182
4ace06e8
MS
2183 if (!hsotg->connected)
2184 return;
2185
2186 hsotg->connected = 0;
9e14d0a5 2187 hsotg->test_mode = 0;
c6f5c050
MYK
2188
2189 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
2190 if (hsotg->eps_in[ep])
2191 kill_all_requests(hsotg, hsotg->eps_in[ep],
2192 -ESHUTDOWN);
2193 if (hsotg->eps_out[ep])
2194 kill_all_requests(hsotg, hsotg->eps_out[ep],
2195 -ESHUTDOWN);
2196 }
5b7d70c6
BD
2197
2198 call_gadget(hsotg, disconnect);
065d3931 2199 hsotg->lx_state = DWC2_L3;
5b7d70c6
BD
2200}
2201
2202/**
1f91b4cc 2203 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
5b7d70c6
BD
2204 * @hsotg: The device state:
2205 * @periodic: True if this is a periodic FIFO interrupt
2206 */
1f91b4cc 2207static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
5b7d70c6 2208{
1f91b4cc 2209 struct dwc2_hsotg_ep *ep;
5b7d70c6
BD
2210 int epno, ret;
2211
2212 /* look through for any more data to transmit */
b3f489b2 2213 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
c6f5c050
MYK
2214 ep = index_to_ep(hsotg, epno, 1);
2215
2216 if (!ep)
2217 continue;
5b7d70c6
BD
2218
2219 if (!ep->dir_in)
2220 continue;
2221
2222 if ((periodic && !ep->periodic) ||
2223 (!periodic && ep->periodic))
2224 continue;
2225
1f91b4cc 2226 ret = dwc2_hsotg_trytx(hsotg, ep);
5b7d70c6
BD
2227 if (ret < 0)
2228 break;
2229 }
2230}
2231
5b7d70c6 2232/* IRQ flags which will trigger a retry around the IRQ loop */
47a1685f
DN
2233#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
2234 GINTSTS_PTXFEMP | \
2235 GINTSTS_RXFLVL)
5b7d70c6 2236
8b9bc460 2237/**
1f91b4cc 2238 * dwc2_hsotg_core_init - issue softreset to the core
8b9bc460
LM
2239 * @hsotg: The device state
2240 *
2241 * Issue a soft reset to the core, and await the core finishing it.
2242 */
1f91b4cc 2243void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
643cc4de 2244 bool is_usb_reset)
308d734e 2245{
1ee6903b 2246 u32 intmsk;
643cc4de 2247 u32 val;
ecd9a7ad 2248 u32 usbcfg;
643cc4de 2249
5390d438
MYK
2250 /* Kill any ep0 requests as controller will be reinitialized */
2251 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
2252
643cc4de 2253 if (!is_usb_reset)
241729ba 2254 if (dwc2_core_reset(hsotg))
86de4895 2255 return;
308d734e
LM
2256
2257 /*
2258 * we must now enable ep0 ready for host detection and then
2259 * set configuration.
2260 */
2261
ecd9a7ad
PR
2262 /* keep other bits untouched (so e.g. forced modes are not lost) */
2263 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2264 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
2265 GUSBCFG_HNPCAP);
2266
308d734e 2267 /* set the PLL on, remove the HNP/SRP and set the PHY */
fa4a8d72 2268 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
ecd9a7ad
PR
2269 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
2270 (val << GUSBCFG_USBTRDTIM_SHIFT);
2271 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
308d734e 2272
1f91b4cc 2273 dwc2_hsotg_init_fifo(hsotg);
308d734e 2274
643cc4de
GH
2275 if (!is_usb_reset)
2276 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
308d734e 2277
95c8bc36 2278 dwc2_writel(DCFG_EPMISCNT(1) | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
308d734e
LM
2279
2280 /* Clear any pending OTG interrupts */
95c8bc36 2281 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
308d734e
LM
2282
2283 /* Clear any pending interrupts */
95c8bc36 2284 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
1ee6903b 2285 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
47a1685f 2286 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
1ee6903b
GH
2287 GINTSTS_USBRST | GINTSTS_RESETDET |
2288 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
ec1f9d9f
RB
2289 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
2290 GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
1ee6903b
GH
2291
2292 if (hsotg->core_params->external_id_pin_ctl <= 0)
2293 intmsk |= GINTSTS_CONIDSTSCHNG;
2294
2295 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
308d734e
LM
2296
2297 if (using_dma(hsotg))
95c8bc36
AS
2298 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
2299 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
2300 hsotg->regs + GAHBCFG);
308d734e 2301 else
95c8bc36
AS
2302 dwc2_writel(((hsotg->dedicated_fifos) ?
2303 (GAHBCFG_NP_TXF_EMP_LVL |
2304 GAHBCFG_P_TXF_EMP_LVL) : 0) |
2305 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
308d734e
LM
2306
2307 /*
8acc8296
RB
2308 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2309 * when we have no data to transfer. Otherwise we get being flooded by
2310 * interrupts.
308d734e
LM
2311 */
2312
95c8bc36 2313 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
6ff2e832 2314 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
47a1685f
DN
2315 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
2316 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2317 DIEPMSK_INTKNEPMISMSK,
2318 hsotg->regs + DIEPMSK);
308d734e
LM
2319
2320 /*
2321 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2322 * DMA mode we may need this.
2323 */
95c8bc36 2324 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
47a1685f
DN
2325 DIEPMSK_TIMEOUTMSK) : 0) |
2326 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
2327 DOEPMSK_SETUPMSK,
2328 hsotg->regs + DOEPMSK);
308d734e 2329
95c8bc36 2330 dwc2_writel(0, hsotg->regs + DAINTMSK);
308d734e
LM
2331
2332 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
95c8bc36
AS
2333 dwc2_readl(hsotg->regs + DIEPCTL0),
2334 dwc2_readl(hsotg->regs + DOEPCTL0));
308d734e
LM
2335
2336 /* enable in and out endpoint interrupts */
1f91b4cc 2337 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
308d734e
LM
2338
2339 /*
2340 * Enable the RXFIFO when in slave mode, as this is how we collect
2341 * the data. In DMA mode, we get events from the FIFO but also
2342 * things we cannot process, so do not use it.
2343 */
2344 if (!using_dma(hsotg))
1f91b4cc 2345 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
308d734e
LM
2346
2347 /* Enable interrupts for EP0 in and out */
1f91b4cc
FB
2348 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2349 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
308d734e 2350
643cc4de
GH
2351 if (!is_usb_reset) {
2352 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2353 udelay(10); /* see openiboot */
2354 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2355 }
308d734e 2356
95c8bc36 2357 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
308d734e
LM
2358
2359 /*
94cb8fd6 2360 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
308d734e
LM
2361 * writing to the EPCTL register..
2362 */
2363
2364 /* set to read 1 8byte packet */
95c8bc36 2365 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
47a1685f 2366 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
308d734e 2367
95c8bc36 2368 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
47a1685f
DN
2369 DXEPCTL_CNAK | DXEPCTL_EPENA |
2370 DXEPCTL_USBACTEP,
94cb8fd6 2371 hsotg->regs + DOEPCTL0);
308d734e
LM
2372
2373 /* enable, but don't activate EP0in */
95c8bc36 2374 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
47a1685f 2375 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
308d734e 2376
1f91b4cc 2377 dwc2_hsotg_enqueue_setup(hsotg);
308d734e
LM
2378
2379 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
95c8bc36
AS
2380 dwc2_readl(hsotg->regs + DIEPCTL0),
2381 dwc2_readl(hsotg->regs + DOEPCTL0));
308d734e
LM
2382
2383 /* clear global NAKs */
643cc4de
GH
2384 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
2385 if (!is_usb_reset)
2386 val |= DCTL_SFTDISCON;
2387 __orr32(hsotg->regs + DCTL, val);
308d734e
LM
2388
2389 /* must be at-least 3ms to allow bus to see disconnect */
2390 mdelay(3);
2391
065d3931 2392 hsotg->lx_state = DWC2_L0;
ad38dc5d
MS
2393}
2394
1f91b4cc 2395static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
ad38dc5d
MS
2396{
2397 /* set the soft-disconnect bit */
2398 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
2399}
ac3c81f3 2400
1f91b4cc 2401void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
ad38dc5d 2402{
308d734e 2403 /* remove the soft-disconnect and let's go */
47a1685f 2404 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
308d734e
LM
2405}
2406
5b7d70c6 2407/**
1f91b4cc 2408 * dwc2_hsotg_irq - handle device interrupt
5b7d70c6
BD
2409 * @irq: The IRQ number triggered
2410 * @pw: The pw value when registered the handler.
2411 */
1f91b4cc 2412static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
5b7d70c6 2413{
941fcce4 2414 struct dwc2_hsotg *hsotg = pw;
5b7d70c6
BD
2415 int retry_count = 8;
2416 u32 gintsts;
2417 u32 gintmsk;
2418
ee3de8d7
VM
2419 if (!dwc2_is_device_mode(hsotg))
2420 return IRQ_NONE;
2421
5ad1d316 2422 spin_lock(&hsotg->lock);
5b7d70c6 2423irq_retry:
95c8bc36
AS
2424 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
2425 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
5b7d70c6
BD
2426
2427 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2428 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2429
2430 gintsts &= gintmsk;
2431
8fc37b82
MYK
2432 if (gintsts & GINTSTS_RESETDET) {
2433 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
2434
2435 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
2436
2437 /* This event must be used only if controller is suspended */
2438 if (hsotg->lx_state == DWC2_L2) {
2439 dwc2_exit_hibernation(hsotg, true);
2440 hsotg->lx_state = DWC2_L0;
2441 }
2442 }
2443
2444 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
2445
2446 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
2447 u32 connected = hsotg->connected;
2448
2449 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
2450 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2451 dwc2_readl(hsotg->regs + GNPTXSTS));
2452
2453 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
2454
2455 /* Report disconnection if it is not already done. */
2456 dwc2_hsotg_disconnect(hsotg);
2457
2458 if (usb_status & GOTGCTL_BSESVLD && connected)
2459 dwc2_hsotg_core_init_disconnected(hsotg, true);
2460 }
2461
47a1685f 2462 if (gintsts & GINTSTS_ENUMDONE) {
95c8bc36 2463 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
a3395f0d 2464
1f91b4cc 2465 dwc2_hsotg_irq_enumdone(hsotg);
5b7d70c6
BD
2466 }
2467
47a1685f 2468 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
95c8bc36
AS
2469 u32 daint = dwc2_readl(hsotg->regs + DAINT);
2470 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
7e804650 2471 u32 daint_out, daint_in;
5b7d70c6
BD
2472 int ep;
2473
7e804650 2474 daint &= daintmsk;
47a1685f
DN
2475 daint_out = daint >> DAINT_OUTEP_SHIFT;
2476 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
7e804650 2477
5b7d70c6
BD
2478 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2479
cec87f1d
MYK
2480 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
2481 ep++, daint_out >>= 1) {
5b7d70c6 2482 if (daint_out & 1)
1f91b4cc 2483 dwc2_hsotg_epint(hsotg, ep, 0);
5b7d70c6
BD
2484 }
2485
cec87f1d
MYK
2486 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
2487 ep++, daint_in >>= 1) {
5b7d70c6 2488 if (daint_in & 1)
1f91b4cc 2489 dwc2_hsotg_epint(hsotg, ep, 1);
5b7d70c6 2490 }
5b7d70c6
BD
2491 }
2492
5b7d70c6
BD
2493 /* check both FIFOs */
2494
47a1685f 2495 if (gintsts & GINTSTS_NPTXFEMP) {
5b7d70c6
BD
2496 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2497
8b9bc460
LM
2498 /*
2499 * Disable the interrupt to stop it happening again
5b7d70c6 2500 * unless one of these endpoint routines decides that
8b9bc460
LM
2501 * it needs re-enabling
2502 */
5b7d70c6 2503
1f91b4cc
FB
2504 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
2505 dwc2_hsotg_irq_fifoempty(hsotg, false);
5b7d70c6
BD
2506 }
2507
47a1685f 2508 if (gintsts & GINTSTS_PTXFEMP) {
5b7d70c6
BD
2509 dev_dbg(hsotg->dev, "PTxFEmp\n");
2510
94cb8fd6 2511 /* See note in GINTSTS_NPTxFEmp */
5b7d70c6 2512
1f91b4cc
FB
2513 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
2514 dwc2_hsotg_irq_fifoempty(hsotg, true);
5b7d70c6
BD
2515 }
2516
47a1685f 2517 if (gintsts & GINTSTS_RXFLVL) {
8b9bc460
LM
2518 /*
2519 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
1f91b4cc 2520 * we need to retry dwc2_hsotg_handle_rx if this is still
8b9bc460
LM
2521 * set.
2522 */
5b7d70c6 2523
1f91b4cc 2524 dwc2_hsotg_handle_rx(hsotg);
5b7d70c6
BD
2525 }
2526
47a1685f 2527 if (gintsts & GINTSTS_ERLYSUSP) {
94cb8fd6 2528 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
95c8bc36 2529 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
5b7d70c6
BD
2530 }
2531
8b9bc460
LM
2532 /*
2533 * these next two seem to crop-up occasionally causing the core
5b7d70c6 2534 * to shutdown the USB transfer, so try clearing them and logging
8b9bc460
LM
2535 * the occurrence.
2536 */
5b7d70c6 2537
47a1685f 2538 if (gintsts & GINTSTS_GOUTNAKEFF) {
5b7d70c6
BD
2539 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2540
3be99cd0 2541 __orr32(hsotg->regs + DCTL, DCTL_CGOUTNAK);
a3395f0d 2542
1f91b4cc 2543 dwc2_hsotg_dump(hsotg);
5b7d70c6
BD
2544 }
2545
47a1685f 2546 if (gintsts & GINTSTS_GINNAKEFF) {
5b7d70c6
BD
2547 dev_info(hsotg->dev, "GINNakEff triggered\n");
2548
3be99cd0 2549 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
a3395f0d 2550
1f91b4cc 2551 dwc2_hsotg_dump(hsotg);
5b7d70c6
BD
2552 }
2553
ec1f9d9f
RB
2554 if (gintsts & GINTSTS_INCOMPL_SOIN) {
2555 u32 idx, epctl_reg;
2556 struct dwc2_hsotg_ep *hs_ep;
2557
2558 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOIN\n", __func__);
2559 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
2560 hs_ep = hsotg->eps_in[idx];
2561
2562 if (!hs_ep->isochronous || hs_ep->has_correct_parity)
2563 continue;
2564
2565 epctl_reg = DIEPCTL(idx);
2566 dwc2_hsotg_change_ep_iso_parity(hsotg, epctl_reg);
2567 }
2568 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
2569 }
2570
2571 if (gintsts & GINTSTS_INCOMPL_SOOUT) {
2572 u32 idx, epctl_reg;
2573 struct dwc2_hsotg_ep *hs_ep;
2574
2575 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
2576 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
2577 hs_ep = hsotg->eps_out[idx];
2578
2579 if (!hs_ep->isochronous || hs_ep->has_correct_parity)
2580 continue;
2581
2582 epctl_reg = DOEPCTL(idx);
2583 dwc2_hsotg_change_ep_iso_parity(hsotg, epctl_reg);
2584 }
2585 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
2586 }
2587
8b9bc460
LM
2588 /*
2589 * if we've had fifo events, we should try and go around the
2590 * loop again to see if there's any point in returning yet.
2591 */
5b7d70c6
BD
2592
2593 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2594 goto irq_retry;
2595
5ad1d316
LM
2596 spin_unlock(&hsotg->lock);
2597
5b7d70c6
BD
2598 return IRQ_HANDLED;
2599}
2600
2601/**
1f91b4cc 2602 * dwc2_hsotg_ep_enable - enable the given endpoint
5b7d70c6
BD
2603 * @ep: The USB endpint to configure
2604 * @desc: The USB endpoint descriptor to configure with.
2605 *
2606 * This is called from the USB gadget code's usb_ep_enable().
8b9bc460 2607 */
1f91b4cc 2608static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
5b7d70c6
BD
2609 const struct usb_endpoint_descriptor *desc)
2610{
1f91b4cc 2611 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 2612 struct dwc2_hsotg *hsotg = hs_ep->parent;
5b7d70c6 2613 unsigned long flags;
ca4c55ad 2614 unsigned int index = hs_ep->index;
5b7d70c6
BD
2615 u32 epctrl_reg;
2616 u32 epctrl;
2617 u32 mps;
ca4c55ad
MYK
2618 unsigned int dir_in;
2619 unsigned int i, val, size;
19c190f9 2620 int ret = 0;
5b7d70c6
BD
2621
2622 dev_dbg(hsotg->dev,
2623 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2624 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2625 desc->wMaxPacketSize, desc->bInterval);
2626
2627 /* not to be called for EP0 */
8c3d6092
VA
2628 if (index == 0) {
2629 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
2630 return -EINVAL;
2631 }
5b7d70c6
BD
2632
2633 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2634 if (dir_in != hs_ep->dir_in) {
2635 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2636 return -EINVAL;
2637 }
2638
29cc8897 2639 mps = usb_endpoint_maxp(desc);
5b7d70c6 2640
1f91b4cc 2641 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
5b7d70c6 2642
94cb8fd6 2643 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
95c8bc36 2644 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
5b7d70c6
BD
2645
2646 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2647 __func__, epctrl, epctrl_reg);
2648
22258f49 2649 spin_lock_irqsave(&hsotg->lock, flags);
5b7d70c6 2650
47a1685f
DN
2651 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
2652 epctrl |= DXEPCTL_MPS(mps);
5b7d70c6 2653
8b9bc460
LM
2654 /*
2655 * mark the endpoint as active, otherwise the core may ignore
2656 * transactions entirely for this endpoint
2657 */
47a1685f 2658 epctrl |= DXEPCTL_USBACTEP;
5b7d70c6 2659
8b9bc460
LM
2660 /*
2661 * set the NAK status on the endpoint, otherwise we might try and
5b7d70c6
BD
2662 * do something with data that we've yet got a request to process
2663 * since the RXFIFO will take data for an endpoint even if the
2664 * size register hasn't been set.
2665 */
2666
47a1685f 2667 epctrl |= DXEPCTL_SNAK;
5b7d70c6
BD
2668
2669 /* update the endpoint state */
1f91b4cc 2670 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, dir_in);
5b7d70c6
BD
2671
2672 /* default, set to non-periodic */
1479e841 2673 hs_ep->isochronous = 0;
5b7d70c6 2674 hs_ep->periodic = 0;
a18ed7b0 2675 hs_ep->halted = 0;
1479e841 2676 hs_ep->interval = desc->bInterval;
ec1f9d9f 2677 hs_ep->has_correct_parity = 0;
5b7d70c6 2678
4fca54aa
RB
2679 if (hs_ep->interval > 1 && hs_ep->mc > 1)
2680 dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
2681
5b7d70c6
BD
2682 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2683 case USB_ENDPOINT_XFER_ISOC:
47a1685f
DN
2684 epctrl |= DXEPCTL_EPTYPE_ISO;
2685 epctrl |= DXEPCTL_SETEVENFR;
1479e841
RB
2686 hs_ep->isochronous = 1;
2687 if (dir_in)
2688 hs_ep->periodic = 1;
2689 break;
5b7d70c6
BD
2690
2691 case USB_ENDPOINT_XFER_BULK:
47a1685f 2692 epctrl |= DXEPCTL_EPTYPE_BULK;
5b7d70c6
BD
2693 break;
2694
2695 case USB_ENDPOINT_XFER_INT:
b203d0a2 2696 if (dir_in)
5b7d70c6 2697 hs_ep->periodic = 1;
5b7d70c6 2698
47a1685f 2699 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
5b7d70c6
BD
2700 break;
2701
2702 case USB_ENDPOINT_XFER_CONTROL:
47a1685f 2703 epctrl |= DXEPCTL_EPTYPE_CONTROL;
5b7d70c6
BD
2704 break;
2705 }
2706
4556e12c
MYK
2707 /* If fifo is already allocated for this ep */
2708 if (hs_ep->fifo_index) {
2709 size = hs_ep->ep.maxpacket * hs_ep->mc;
2710 /* If bigger fifo is required deallocate current one */
2711 if (size > hs_ep->fifo_size) {
2712 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
2713 hs_ep->fifo_index = 0;
2714 hs_ep->fifo_size = 0;
2715 }
2716 }
2717
8b9bc460
LM
2718 /*
2719 * if the hardware has dedicated fifos, we must give each IN EP
10aebc77
BD
2720 * a unique tx-fifo even if it is non-periodic.
2721 */
4556e12c 2722 if (dir_in && hsotg->dedicated_fifos && !hs_ep->fifo_index) {
ca4c55ad
MYK
2723 u32 fifo_index = 0;
2724 u32 fifo_size = UINT_MAX;
b203d0a2 2725 size = hs_ep->ep.maxpacket*hs_ep->mc;
5f2196bd 2726 for (i = 1; i < hsotg->num_of_eps; ++i) {
b203d0a2
RB
2727 if (hsotg->fifo_map & (1<<i))
2728 continue;
95c8bc36 2729 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
b203d0a2
RB
2730 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
2731 if (val < size)
2732 continue;
ca4c55ad
MYK
2733 /* Search for smallest acceptable fifo */
2734 if (val < fifo_size) {
2735 fifo_size = val;
2736 fifo_index = i;
2737 }
b203d0a2 2738 }
ca4c55ad 2739 if (!fifo_index) {
5f2196bd
MYK
2740 dev_err(hsotg->dev,
2741 "%s: No suitable fifo found\n", __func__);
b585a48b
SM
2742 ret = -ENOMEM;
2743 goto error;
2744 }
ca4c55ad
MYK
2745 hsotg->fifo_map |= 1 << fifo_index;
2746 epctrl |= DXEPCTL_TXFNUM(fifo_index);
2747 hs_ep->fifo_index = fifo_index;
2748 hs_ep->fifo_size = fifo_size;
b203d0a2 2749 }
10aebc77 2750
5b7d70c6
BD
2751 /* for non control endpoints, set PID to D0 */
2752 if (index)
47a1685f 2753 epctrl |= DXEPCTL_SETD0PID;
5b7d70c6
BD
2754
2755 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2756 __func__, epctrl);
2757
95c8bc36 2758 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
5b7d70c6 2759 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
95c8bc36 2760 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
5b7d70c6
BD
2761
2762 /* enable the endpoint interrupt */
1f91b4cc 2763 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
5b7d70c6 2764
b585a48b 2765error:
22258f49 2766 spin_unlock_irqrestore(&hsotg->lock, flags);
19c190f9 2767 return ret;
5b7d70c6
BD
2768}
2769
8b9bc460 2770/**
1f91b4cc 2771 * dwc2_hsotg_ep_disable - disable given endpoint
8b9bc460
LM
2772 * @ep: The endpoint to disable.
2773 */
1f91b4cc 2774static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
5b7d70c6 2775{
1f91b4cc 2776 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 2777 struct dwc2_hsotg *hsotg = hs_ep->parent;
5b7d70c6
BD
2778 int dir_in = hs_ep->dir_in;
2779 int index = hs_ep->index;
2780 unsigned long flags;
2781 u32 epctrl_reg;
2782 u32 ctrl;
2783
1e011293 2784 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
5b7d70c6 2785
c6f5c050 2786 if (ep == &hsotg->eps_out[0]->ep) {
5b7d70c6
BD
2787 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2788 return -EINVAL;
2789 }
2790
94cb8fd6 2791 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
5b7d70c6 2792
5ad1d316 2793 spin_lock_irqsave(&hsotg->lock, flags);
5b7d70c6 2794
b203d0a2
RB
2795 hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
2796 hs_ep->fifo_index = 0;
2797 hs_ep->fifo_size = 0;
5b7d70c6 2798
95c8bc36 2799 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
47a1685f
DN
2800 ctrl &= ~DXEPCTL_EPENA;
2801 ctrl &= ~DXEPCTL_USBACTEP;
2802 ctrl |= DXEPCTL_SNAK;
5b7d70c6
BD
2803
2804 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
95c8bc36 2805 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
5b7d70c6
BD
2806
2807 /* disable endpoint interrupts */
1f91b4cc 2808 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
5b7d70c6 2809
1141ea01
MYK
2810 /* terminate all requests with shutdown */
2811 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
2812
22258f49 2813 spin_unlock_irqrestore(&hsotg->lock, flags);
5b7d70c6
BD
2814 return 0;
2815}
2816
2817/**
2818 * on_list - check request is on the given endpoint
2819 * @ep: The endpoint to check.
2820 * @test: The request to test if it is on the endpoint.
8b9bc460 2821 */
1f91b4cc 2822static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
5b7d70c6 2823{
1f91b4cc 2824 struct dwc2_hsotg_req *req, *treq;
5b7d70c6
BD
2825
2826 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2827 if (req == test)
2828 return true;
2829 }
2830
2831 return false;
2832}
2833
c524dd5f
MYK
2834static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
2835 u32 bit, u32 timeout)
2836{
2837 u32 i;
2838
2839 for (i = 0; i < timeout; i++) {
2840 if (dwc2_readl(hs_otg->regs + reg) & bit)
2841 return 0;
2842 udelay(1);
2843 }
2844
2845 return -ETIMEDOUT;
2846}
2847
2848static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
2849 struct dwc2_hsotg_ep *hs_ep)
2850{
2851 u32 epctrl_reg;
2852 u32 epint_reg;
2853
2854 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
2855 DOEPCTL(hs_ep->index);
2856 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
2857 DOEPINT(hs_ep->index);
2858
2859 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
2860 hs_ep->name);
2861 if (hs_ep->dir_in) {
2862 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
2863 /* Wait for Nak effect */
2864 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
2865 DXEPINT_INEPNAKEFF, 100))
2866 dev_warn(hsotg->dev,
2867 "%s: timeout DIEPINT.NAKEFF\n", __func__);
2868 } else {
2869 /* Clear any pending nak effect interrupt */
0676c7e7 2870 dwc2_writel(GINTSTS_GOUTNAKEFF, hsotg->regs + GINTSTS);
c524dd5f 2871
0676c7e7 2872 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
c524dd5f
MYK
2873
2874 /* Wait for global nak to take effect */
2875 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
0676c7e7 2876 GINTSTS_GOUTNAKEFF, 100))
c524dd5f 2877 dev_warn(hsotg->dev,
0676c7e7 2878 "%s: timeout GINTSTS.GOUTNAKEFF\n", __func__);
c524dd5f
MYK
2879 }
2880
2881 /* Disable ep */
2882 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
2883
2884 /* Wait for ep to be disabled */
2885 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
2886 dev_warn(hsotg->dev,
2887 "%s: timeout DOEPCTL.EPDisable\n", __func__);
2888
2889 if (hs_ep->dir_in) {
2890 if (hsotg->dedicated_fifos) {
2891 dwc2_writel(GRSTCTL_TXFNUM(hs_ep->fifo_index) |
2892 GRSTCTL_TXFFLSH, hsotg->regs + GRSTCTL);
2893 /* Wait for fifo flush */
2894 if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL,
2895 GRSTCTL_TXFFLSH, 100))
2896 dev_warn(hsotg->dev,
2897 "%s: timeout flushing fifos\n",
2898 __func__);
2899 }
2900 /* TODO: Flush shared tx fifo */
2901 } else {
2902 /* Remove global NAKs */
0676c7e7 2903 __bic32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
c524dd5f
MYK
2904 }
2905}
2906
8b9bc460 2907/**
1f91b4cc 2908 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
8b9bc460
LM
2909 * @ep: The endpoint to dequeue.
2910 * @req: The request to be removed from a queue.
2911 */
1f91b4cc 2912static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
5b7d70c6 2913{
1f91b4cc
FB
2914 struct dwc2_hsotg_req *hs_req = our_req(req);
2915 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 2916 struct dwc2_hsotg *hs = hs_ep->parent;
5b7d70c6
BD
2917 unsigned long flags;
2918
1e011293 2919 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
5b7d70c6 2920
22258f49 2921 spin_lock_irqsave(&hs->lock, flags);
5b7d70c6
BD
2922
2923 if (!on_list(hs_ep, hs_req)) {
22258f49 2924 spin_unlock_irqrestore(&hs->lock, flags);
5b7d70c6
BD
2925 return -EINVAL;
2926 }
2927
c524dd5f
MYK
2928 /* Dequeue already started request */
2929 if (req == &hs_ep->req->req)
2930 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
2931
1f91b4cc 2932 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
22258f49 2933 spin_unlock_irqrestore(&hs->lock, flags);
5b7d70c6
BD
2934
2935 return 0;
2936}
2937
8b9bc460 2938/**
1f91b4cc 2939 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
8b9bc460
LM
2940 * @ep: The endpoint to set halt.
2941 * @value: Set or unset the halt.
51da43b5
VA
2942 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
2943 * the endpoint is busy processing requests.
2944 *
2945 * We need to stall the endpoint immediately if request comes from set_feature
2946 * protocol command handler.
8b9bc460 2947 */
51da43b5 2948static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
5b7d70c6 2949{
1f91b4cc 2950 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 2951 struct dwc2_hsotg *hs = hs_ep->parent;
5b7d70c6 2952 int index = hs_ep->index;
5b7d70c6
BD
2953 u32 epreg;
2954 u32 epctl;
9c39ddc6 2955 u32 xfertype;
5b7d70c6
BD
2956
2957 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2958
c9f721b2
RB
2959 if (index == 0) {
2960 if (value)
1f91b4cc 2961 dwc2_hsotg_stall_ep0(hs);
c9f721b2
RB
2962 else
2963 dev_warn(hs->dev,
2964 "%s: can't clear halt on ep0\n", __func__);
2965 return 0;
2966 }
2967
15186f10
VA
2968 if (hs_ep->isochronous) {
2969 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
2970 return -EINVAL;
2971 }
2972
51da43b5
VA
2973 if (!now && value && !list_empty(&hs_ep->queue)) {
2974 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
2975 ep->name);
2976 return -EAGAIN;
2977 }
2978
c6f5c050
MYK
2979 if (hs_ep->dir_in) {
2980 epreg = DIEPCTL(index);
95c8bc36 2981 epctl = dwc2_readl(hs->regs + epreg);
c6f5c050
MYK
2982
2983 if (value) {
5a350d53 2984 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
c6f5c050
MYK
2985 if (epctl & DXEPCTL_EPENA)
2986 epctl |= DXEPCTL_EPDIS;
2987 } else {
2988 epctl &= ~DXEPCTL_STALL;
2989 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2990 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2991 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2992 epctl |= DXEPCTL_SETD0PID;
2993 }
95c8bc36 2994 dwc2_writel(epctl, hs->regs + epreg);
9c39ddc6 2995 } else {
5b7d70c6 2996
c6f5c050 2997 epreg = DOEPCTL(index);
95c8bc36 2998 epctl = dwc2_readl(hs->regs + epreg);
5b7d70c6 2999
c6f5c050
MYK
3000 if (value)
3001 epctl |= DXEPCTL_STALL;
3002 else {
3003 epctl &= ~DXEPCTL_STALL;
3004 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
3005 if (xfertype == DXEPCTL_EPTYPE_BULK ||
3006 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
3007 epctl |= DXEPCTL_SETD0PID;
3008 }
95c8bc36 3009 dwc2_writel(epctl, hs->regs + epreg);
9c39ddc6 3010 }
5b7d70c6 3011
a18ed7b0
RB
3012 hs_ep->halted = value;
3013
5b7d70c6
BD
3014 return 0;
3015}
3016
5ad1d316 3017/**
1f91b4cc 3018 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
5ad1d316
LM
3019 * @ep: The endpoint to set halt.
3020 * @value: Set or unset the halt.
3021 */
1f91b4cc 3022static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
5ad1d316 3023{
1f91b4cc 3024 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
941fcce4 3025 struct dwc2_hsotg *hs = hs_ep->parent;
5ad1d316
LM
3026 unsigned long flags = 0;
3027 int ret = 0;
3028
3029 spin_lock_irqsave(&hs->lock, flags);
51da43b5 3030 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
5ad1d316
LM
3031 spin_unlock_irqrestore(&hs->lock, flags);
3032
3033 return ret;
3034}
3035
1f91b4cc
FB
3036static struct usb_ep_ops dwc2_hsotg_ep_ops = {
3037 .enable = dwc2_hsotg_ep_enable,
3038 .disable = dwc2_hsotg_ep_disable,
3039 .alloc_request = dwc2_hsotg_ep_alloc_request,
3040 .free_request = dwc2_hsotg_ep_free_request,
3041 .queue = dwc2_hsotg_ep_queue_lock,
3042 .dequeue = dwc2_hsotg_ep_dequeue,
3043 .set_halt = dwc2_hsotg_ep_sethalt_lock,
25985edc 3044 /* note, don't believe we have any call for the fifo routines */
5b7d70c6
BD
3045};
3046
8b9bc460 3047/**
1f91b4cc 3048 * dwc2_hsotg_init - initalize the usb core
8b9bc460
LM
3049 * @hsotg: The driver state
3050 */
1f91b4cc 3051static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
b3f489b2 3052{
fa4a8d72 3053 u32 trdtim;
ecd9a7ad 3054 u32 usbcfg;
b3f489b2
LM
3055 /* unmask subset of endpoint interrupts */
3056
95c8bc36
AS
3057 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
3058 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
3059 hsotg->regs + DIEPMSK);
b3f489b2 3060
95c8bc36
AS
3061 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
3062 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
3063 hsotg->regs + DOEPMSK);
b3f489b2 3064
95c8bc36 3065 dwc2_writel(0, hsotg->regs + DAINTMSK);
b3f489b2
LM
3066
3067 /* Be in disconnected state until gadget is registered */
47a1685f 3068 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
b3f489b2 3069
b3f489b2
LM
3070 /* setup fifos */
3071
3072 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
95c8bc36
AS
3073 dwc2_readl(hsotg->regs + GRXFSIZ),
3074 dwc2_readl(hsotg->regs + GNPTXFSIZ));
b3f489b2 3075
1f91b4cc 3076 dwc2_hsotg_init_fifo(hsotg);
b3f489b2 3077
ecd9a7ad
PR
3078 /* keep other bits untouched (so e.g. forced modes are not lost) */
3079 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3080 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
3081 GUSBCFG_HNPCAP);
3082
b3f489b2 3083 /* set the PLL on, remove the HNP/SRP and set the PHY */
fa4a8d72 3084 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
ecd9a7ad
PR
3085 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3086 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
3087 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
b3f489b2 3088
f5090044
GH
3089 if (using_dma(hsotg))
3090 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
b3f489b2
LM
3091}
3092
8b9bc460 3093/**
1f91b4cc 3094 * dwc2_hsotg_udc_start - prepare the udc for work
8b9bc460
LM
3095 * @gadget: The usb gadget state
3096 * @driver: The usb gadget driver
3097 *
3098 * Perform initialization to prepare udc device and driver
3099 * to work.
3100 */
1f91b4cc 3101static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
f65f0f10 3102 struct usb_gadget_driver *driver)
5b7d70c6 3103{
941fcce4 3104 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
5b9451f8 3105 unsigned long flags;
5b7d70c6
BD
3106 int ret;
3107
3108 if (!hsotg) {
a023da33 3109 pr_err("%s: called with no device\n", __func__);
5b7d70c6
BD
3110 return -ENODEV;
3111 }
3112
3113 if (!driver) {
3114 dev_err(hsotg->dev, "%s: no driver\n", __func__);
3115 return -EINVAL;
3116 }
3117
7177aed4 3118 if (driver->max_speed < USB_SPEED_FULL)
5b7d70c6 3119 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
5b7d70c6 3120
f65f0f10 3121 if (!driver->setup) {
5b7d70c6
BD
3122 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
3123 return -EINVAL;
3124 }
3125
3126 WARN_ON(hsotg->driver);
3127
3128 driver->driver.bus = NULL;
3129 hsotg->driver = driver;
7d7b2292 3130 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
5b7d70c6
BD
3131 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3132
09a75e85
MS
3133 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
3134 ret = dwc2_lowlevel_hw_enable(hsotg);
3135 if (ret)
3136 goto err;
5b7d70c6
BD
3137 }
3138
f6c01592
GH
3139 if (!IS_ERR_OR_NULL(hsotg->uphy))
3140 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
c816c47f 3141
5b9451f8 3142 spin_lock_irqsave(&hsotg->lock, flags);
1f91b4cc
FB
3143 dwc2_hsotg_init(hsotg);
3144 dwc2_hsotg_core_init_disconnected(hsotg, false);
dc6e69e6 3145 hsotg->enabled = 0;
5b9451f8
MS
3146 spin_unlock_irqrestore(&hsotg->lock, flags);
3147
5b7d70c6 3148 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
5b9451f8 3149
5b7d70c6
BD
3150 return 0;
3151
3152err:
3153 hsotg->driver = NULL;
5b7d70c6
BD
3154 return ret;
3155}
3156
8b9bc460 3157/**
1f91b4cc 3158 * dwc2_hsotg_udc_stop - stop the udc
8b9bc460
LM
3159 * @gadget: The usb gadget state
3160 * @driver: The usb gadget driver
3161 *
3162 * Stop udc hw block and stay tunned for future transmissions
3163 */
1f91b4cc 3164static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
5b7d70c6 3165{
941fcce4 3166 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
2b19a52c 3167 unsigned long flags = 0;
5b7d70c6
BD
3168 int ep;
3169
3170 if (!hsotg)
3171 return -ENODEV;
3172
5b7d70c6 3173 /* all endpoints should be shutdown */
c6f5c050
MYK
3174 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3175 if (hsotg->eps_in[ep])
1f91b4cc 3176 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
c6f5c050 3177 if (hsotg->eps_out[ep])
1f91b4cc 3178 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
c6f5c050 3179 }
5b7d70c6 3180
2b19a52c
LM
3181 spin_lock_irqsave(&hsotg->lock, flags);
3182
32805c35 3183 hsotg->driver = NULL;
5b7d70c6 3184 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
dc6e69e6 3185 hsotg->enabled = 0;
5b7d70c6 3186
2b19a52c
LM
3187 spin_unlock_irqrestore(&hsotg->lock, flags);
3188
f6c01592
GH
3189 if (!IS_ERR_OR_NULL(hsotg->uphy))
3190 otg_set_peripheral(hsotg->uphy->otg, NULL);
c816c47f 3191
09a75e85
MS
3192 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
3193 dwc2_lowlevel_hw_disable(hsotg);
5b7d70c6
BD
3194
3195 return 0;
3196}
5b7d70c6 3197
8b9bc460 3198/**
1f91b4cc 3199 * dwc2_hsotg_gadget_getframe - read the frame number
8b9bc460
LM
3200 * @gadget: The usb gadget state
3201 *
3202 * Read the {micro} frame number
3203 */
1f91b4cc 3204static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
5b7d70c6 3205{
1f91b4cc 3206 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
5b7d70c6
BD
3207}
3208
a188b689 3209/**
1f91b4cc 3210 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
a188b689
LM
3211 * @gadget: The usb gadget state
3212 * @is_on: Current state of the USB PHY
3213 *
3214 * Connect/Disconnect the USB PHY pullup
3215 */
1f91b4cc 3216static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
a188b689 3217{
941fcce4 3218 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
a188b689
LM
3219 unsigned long flags = 0;
3220
77ba9119
GH
3221 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
3222 hsotg->op_state);
3223
3224 /* Don't modify pullup state while in host mode */
3225 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
3226 hsotg->enabled = is_on;
3227 return 0;
3228 }
a188b689
LM
3229
3230 spin_lock_irqsave(&hsotg->lock, flags);
3231 if (is_on) {
dc6e69e6 3232 hsotg->enabled = 1;
1f91b4cc
FB
3233 dwc2_hsotg_core_init_disconnected(hsotg, false);
3234 dwc2_hsotg_core_connect(hsotg);
a188b689 3235 } else {
1f91b4cc
FB
3236 dwc2_hsotg_core_disconnect(hsotg);
3237 dwc2_hsotg_disconnect(hsotg);
dc6e69e6 3238 hsotg->enabled = 0;
a188b689
LM
3239 }
3240
3241 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3242 spin_unlock_irqrestore(&hsotg->lock, flags);
3243
3244 return 0;
3245}
3246
1f91b4cc 3247static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
83d98223
GH
3248{
3249 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3250 unsigned long flags;
3251
3252 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
3253 spin_lock_irqsave(&hsotg->lock, flags);
3254
61f7223b
GH
3255 /*
3256 * If controller is hibernated, it must exit from hibernation
3257 * before being initialized / de-initialized
3258 */
3259 if (hsotg->lx_state == DWC2_L2)
3260 dwc2_exit_hibernation(hsotg, false);
3261
83d98223 3262 if (is_active) {
cd0e641c 3263 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
065d3931 3264
1f91b4cc 3265 dwc2_hsotg_core_init_disconnected(hsotg, false);
83d98223 3266 if (hsotg->enabled)
1f91b4cc 3267 dwc2_hsotg_core_connect(hsotg);
83d98223 3268 } else {
1f91b4cc
FB
3269 dwc2_hsotg_core_disconnect(hsotg);
3270 dwc2_hsotg_disconnect(hsotg);
83d98223
GH
3271 }
3272
3273 spin_unlock_irqrestore(&hsotg->lock, flags);
3274 return 0;
3275}
3276
596d696a 3277/**
1f91b4cc 3278 * dwc2_hsotg_vbus_draw - report bMaxPower field
596d696a
GH
3279 * @gadget: The usb gadget state
3280 * @mA: Amount of current
3281 *
3282 * Report how much power the device may consume to the phy.
3283 */
1f91b4cc 3284static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
596d696a
GH
3285{
3286 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3287
3288 if (IS_ERR_OR_NULL(hsotg->uphy))
3289 return -ENOTSUPP;
3290 return usb_phy_set_power(hsotg->uphy, mA);
3291}
3292
1f91b4cc
FB
3293static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
3294 .get_frame = dwc2_hsotg_gadget_getframe,
3295 .udc_start = dwc2_hsotg_udc_start,
3296 .udc_stop = dwc2_hsotg_udc_stop,
3297 .pullup = dwc2_hsotg_pullup,
3298 .vbus_session = dwc2_hsotg_vbus_session,
3299 .vbus_draw = dwc2_hsotg_vbus_draw,
5b7d70c6
BD
3300};
3301
3302/**
1f91b4cc 3303 * dwc2_hsotg_initep - initialise a single endpoint
5b7d70c6
BD
3304 * @hsotg: The device state.
3305 * @hs_ep: The endpoint to be initialised.
3306 * @epnum: The endpoint number
3307 *
3308 * Initialise the given endpoint (as part of the probe and device state
3309 * creation) to give to the gadget driver. Setup the endpoint name, any
3310 * direction information and other state that may be required.
3311 */
1f91b4cc
FB
3312static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
3313 struct dwc2_hsotg_ep *hs_ep,
c6f5c050
MYK
3314 int epnum,
3315 bool dir_in)
5b7d70c6 3316{
5b7d70c6
BD
3317 char *dir;
3318
3319 if (epnum == 0)
3320 dir = "";
c6f5c050 3321 else if (dir_in)
5b7d70c6 3322 dir = "in";
c6f5c050
MYK
3323 else
3324 dir = "out";
5b7d70c6 3325
c6f5c050 3326 hs_ep->dir_in = dir_in;
5b7d70c6
BD
3327 hs_ep->index = epnum;
3328
3329 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3330
3331 INIT_LIST_HEAD(&hs_ep->queue);
3332 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3333
5b7d70c6
BD
3334 /* add to the list of endpoints known by the gadget driver */
3335 if (epnum)
3336 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3337
3338 hs_ep->parent = hsotg;
3339 hs_ep->ep.name = hs_ep->name;
e117e742 3340 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
1f91b4cc 3341 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
5b7d70c6 3342
2954522f
RB
3343 if (epnum == 0) {
3344 hs_ep->ep.caps.type_control = true;
3345 } else {
3346 hs_ep->ep.caps.type_iso = true;
3347 hs_ep->ep.caps.type_bulk = true;
3348 hs_ep->ep.caps.type_int = true;
3349 }
3350
3351 if (dir_in)
3352 hs_ep->ep.caps.dir_in = true;
3353 else
3354 hs_ep->ep.caps.dir_out = true;
3355
8b9bc460
LM
3356 /*
3357 * if we're using dma, we need to set the next-endpoint pointer
5b7d70c6
BD
3358 * to be something valid.
3359 */
3360
3361 if (using_dma(hsotg)) {
47a1685f 3362 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
c6f5c050 3363 if (dir_in)
95c8bc36 3364 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
c6f5c050 3365 else
95c8bc36 3366 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
5b7d70c6
BD
3367 }
3368}
3369
b3f489b2 3370/**
1f91b4cc 3371 * dwc2_hsotg_hw_cfg - read HW configuration registers
b3f489b2
LM
3372 * @param: The device state
3373 *
3374 * Read the USB core HW configuration registers
3375 */
1f91b4cc 3376static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
5b7d70c6 3377{
c6f5c050
MYK
3378 u32 cfg;
3379 u32 ep_type;
3380 u32 i;
3381
b3f489b2 3382 /* check hardware configuration */
5b7d70c6 3383
43e90349
JY
3384 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
3385
c6f5c050
MYK
3386 /* Add ep0 */
3387 hsotg->num_of_eps++;
10aebc77 3388
1f91b4cc 3389 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct dwc2_hsotg_ep),
c6f5c050
MYK
3390 GFP_KERNEL);
3391 if (!hsotg->eps_in[0])
3392 return -ENOMEM;
1f91b4cc 3393 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
c6f5c050
MYK
3394 hsotg->eps_out[0] = hsotg->eps_in[0];
3395
43e90349 3396 cfg = hsotg->hw_params.dev_ep_dirs;
251a17f5 3397 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
c6f5c050
MYK
3398 ep_type = cfg & 3;
3399 /* Direction in or both */
3400 if (!(ep_type & 2)) {
3401 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
1f91b4cc 3402 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
c6f5c050
MYK
3403 if (!hsotg->eps_in[i])
3404 return -ENOMEM;
3405 }
3406 /* Direction out or both */
3407 if (!(ep_type & 1)) {
3408 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
1f91b4cc 3409 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
c6f5c050
MYK
3410 if (!hsotg->eps_out[i])
3411 return -ENOMEM;
3412 }
3413 }
3414
43e90349
JY
3415 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
3416 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
10aebc77 3417
cff9eb75
MS
3418 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
3419 hsotg->num_of_eps,
3420 hsotg->dedicated_fifos ? "dedicated" : "shared",
3421 hsotg->fifo_mem);
c6f5c050 3422 return 0;
5b7d70c6
BD
3423}
3424
8b9bc460 3425/**
1f91b4cc 3426 * dwc2_hsotg_dump - dump state of the udc
8b9bc460
LM
3427 * @param: The device state
3428 */
1f91b4cc 3429static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
5b7d70c6 3430{
83a01804 3431#ifdef DEBUG
5b7d70c6
BD
3432 struct device *dev = hsotg->dev;
3433 void __iomem *regs = hsotg->regs;
3434 u32 val;
3435 int idx;
3436
3437 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
95c8bc36
AS
3438 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
3439 dwc2_readl(regs + DIEPMSK));
5b7d70c6 3440
f889f23d 3441 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
95c8bc36 3442 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
5b7d70c6
BD
3443
3444 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
95c8bc36 3445 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
5b7d70c6
BD
3446
3447 /* show periodic fifo settings */
3448
364f8e93 3449 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
95c8bc36 3450 val = dwc2_readl(regs + DPTXFSIZN(idx));
5b7d70c6 3451 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
47a1685f
DN
3452 val >> FIFOSIZE_DEPTH_SHIFT,
3453 val & FIFOSIZE_STARTADDR_MASK);
5b7d70c6
BD
3454 }
3455
364f8e93 3456 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
5b7d70c6
BD
3457 dev_info(dev,
3458 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
95c8bc36
AS
3459 dwc2_readl(regs + DIEPCTL(idx)),
3460 dwc2_readl(regs + DIEPTSIZ(idx)),
3461 dwc2_readl(regs + DIEPDMA(idx)));
5b7d70c6 3462
95c8bc36 3463 val = dwc2_readl(regs + DOEPCTL(idx));
5b7d70c6
BD
3464 dev_info(dev,
3465 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
95c8bc36
AS
3466 idx, dwc2_readl(regs + DOEPCTL(idx)),
3467 dwc2_readl(regs + DOEPTSIZ(idx)),
3468 dwc2_readl(regs + DOEPDMA(idx)));
5b7d70c6
BD
3469
3470 }
3471
3472 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
95c8bc36 3473 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
83a01804 3474#endif
5b7d70c6
BD
3475}
3476
edd74be8 3477#ifdef CONFIG_OF
1f91b4cc 3478static void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg)
edd74be8
GH
3479{
3480 struct device_node *np = hsotg->dev->of_node;
0a176279
GH
3481 u32 len = 0;
3482 u32 i = 0;
edd74be8
GH
3483
3484 /* Enable dma if requested in device tree */
3485 hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma");
0a176279
GH
3486
3487 /*
3488 * Register TX periodic fifo size per endpoint.
3489 * EP0 is excluded since it has no fifo configuration.
3490 */
3491 if (!of_find_property(np, "g-tx-fifo-size", &len))
3492 goto rx_fifo;
3493
3494 len /= sizeof(u32);
3495
3496 /* Read tx fifo sizes other than ep0 */
3497 if (of_property_read_u32_array(np, "g-tx-fifo-size",
3498 &hsotg->g_tx_fifo_sz[1], len))
3499 goto rx_fifo;
3500
3501 /* Add ep0 */
3502 len++;
3503
3504 /* Make remaining TX fifos unavailable */
3505 if (len < MAX_EPS_CHANNELS) {
3506 for (i = len; i < MAX_EPS_CHANNELS; i++)
3507 hsotg->g_tx_fifo_sz[i] = 0;
3508 }
3509
3510rx_fifo:
3511 /* Register RX fifo size */
3512 of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz);
3513
3514 /* Register NPTX fifo size */
3515 of_property_read_u32(np, "g-np-tx-fifo-size",
3516 &hsotg->g_np_g_tx_fifo_sz);
edd74be8
GH
3517}
3518#else
1f91b4cc 3519static inline void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg) { }
edd74be8
GH
3520#endif
3521
8b9bc460 3522/**
117777b2
DN
3523 * dwc2_gadget_init - init function for gadget
3524 * @dwc2: The data structure for the DWC2 driver.
3525 * @irq: The IRQ number for the controller.
8b9bc460 3526 */
117777b2 3527int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
5b7d70c6 3528{
117777b2 3529 struct device *dev = hsotg->dev;
5b7d70c6
BD
3530 int epnum;
3531 int ret;
fc9a731e 3532 int i;
0a176279 3533 u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE;
5b7d70c6 3534
0a176279
GH
3535 /* Initialize to legacy fifo configuration values */
3536 hsotg->g_rx_fifo_sz = 2048;
3537 hsotg->g_np_g_tx_fifo_sz = 1024;
3538 memcpy(&hsotg->g_tx_fifo_sz[1], p_tx_fifo, sizeof(p_tx_fifo));
3539 /* Device tree specific probe */
1f91b4cc 3540 dwc2_hsotg_of_probe(hsotg);
43e90349
JY
3541
3542 /* Check against largest possible value. */
3543 if (hsotg->g_np_g_tx_fifo_sz >
3544 hsotg->hw_params.dev_nperio_tx_fifo_size) {
3545 dev_warn(dev, "Specified GNPTXFDEP=%d > %d\n",
3546 hsotg->g_np_g_tx_fifo_sz,
3547 hsotg->hw_params.dev_nperio_tx_fifo_size);
3548 hsotg->g_np_g_tx_fifo_sz =
3549 hsotg->hw_params.dev_nperio_tx_fifo_size;
3550 }
3551
0a176279
GH
3552 /* Dump fifo information */
3553 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
3554 hsotg->g_np_g_tx_fifo_sz);
3555 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz);
3556 for (i = 0; i < MAX_EPS_CHANNELS; i++)
3557 dev_dbg(dev, "Periodic TXFIFO%2d size: %d\n", i,
3558 hsotg->g_tx_fifo_sz[i]);
5b7d70c6 3559
d327ab5b 3560 hsotg->gadget.max_speed = USB_SPEED_HIGH;
1f91b4cc 3561 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
5b7d70c6 3562 hsotg->gadget.name = dev_name(dev);
097ee662
GH
3563 if (hsotg->dr_mode == USB_DR_MODE_OTG)
3564 hsotg->gadget.is_otg = 1;
ec4cc657
MYK
3565 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
3566 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
5b7d70c6 3567
1f91b4cc 3568 ret = dwc2_hsotg_hw_cfg(hsotg);
c6f5c050
MYK
3569 if (ret) {
3570 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
09a75e85 3571 return ret;
c6f5c050
MYK
3572 }
3573
3f95001d
MYK
3574 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
3575 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
3576 if (!hsotg->ctrl_buff) {
3577 dev_err(dev, "failed to allocate ctrl request buff\n");
09a75e85 3578 return -ENOMEM;
3f95001d
MYK
3579 }
3580
3581 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
3582 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
3583 if (!hsotg->ep0_buff) {
3584 dev_err(dev, "failed to allocate ctrl reply buff\n");
09a75e85 3585 return -ENOMEM;
3f95001d
MYK
3586 }
3587
1f91b4cc 3588 ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
db8178c3 3589 dev_name(hsotg->dev), hsotg);
eb3c56c5 3590 if (ret < 0) {
db8178c3 3591 dev_err(dev, "cannot claim IRQ for gadget\n");
09a75e85 3592 return ret;
eb3c56c5
MS
3593 }
3594
b3f489b2
LM
3595 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3596
3597 if (hsotg->num_of_eps == 0) {
3598 dev_err(dev, "wrong number of EPs (zero)\n");
09a75e85 3599 return -EINVAL;
b3f489b2
LM
3600 }
3601
b3f489b2
LM
3602 /* setup endpoint information */
3603
3604 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
c6f5c050 3605 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
b3f489b2
LM
3606
3607 /* allocate EP0 request */
3608
1f91b4cc 3609 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
b3f489b2
LM
3610 GFP_KERNEL);
3611 if (!hsotg->ctrl_req) {
3612 dev_err(dev, "failed to allocate ctrl req\n");
09a75e85 3613 return -ENOMEM;
b3f489b2 3614 }
5b7d70c6
BD
3615
3616 /* initialise the endpoints now the core has been initialised */
c6f5c050
MYK
3617 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
3618 if (hsotg->eps_in[epnum])
1f91b4cc 3619 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
c6f5c050
MYK
3620 epnum, 1);
3621 if (hsotg->eps_out[epnum])
1f91b4cc 3622 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
c6f5c050
MYK
3623 epnum, 0);
3624 }
5b7d70c6 3625
117777b2 3626 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
0f91349b 3627 if (ret)
09a75e85 3628 return ret;
0f91349b 3629
1f91b4cc 3630 dwc2_hsotg_dump(hsotg);
5b7d70c6 3631
5b7d70c6 3632 return 0;
5b7d70c6
BD
3633}
3634
8b9bc460 3635/**
1f91b4cc 3636 * dwc2_hsotg_remove - remove function for hsotg driver
8b9bc460
LM
3637 * @pdev: The platform information for the driver
3638 */
1f91b4cc 3639int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
5b7d70c6 3640{
0f91349b 3641 usb_del_gadget_udc(&hsotg->gadget);
31ee04de 3642
5b7d70c6
BD
3643 return 0;
3644}
3645
1f91b4cc 3646int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
b83e333a 3647{
b83e333a 3648 unsigned long flags;
b83e333a 3649
9e779778 3650 if (hsotg->lx_state != DWC2_L0)
09a75e85 3651 return 0;
9e779778 3652
dc6e69e6
MS
3653 if (hsotg->driver) {
3654 int ep;
3655
b83e333a
MS
3656 dev_info(hsotg->dev, "suspending usb gadget %s\n",
3657 hsotg->driver->driver.name);
3658
dc6e69e6
MS
3659 spin_lock_irqsave(&hsotg->lock, flags);
3660 if (hsotg->enabled)
1f91b4cc
FB
3661 dwc2_hsotg_core_disconnect(hsotg);
3662 dwc2_hsotg_disconnect(hsotg);
dc6e69e6
MS
3663 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3664 spin_unlock_irqrestore(&hsotg->lock, flags);
b83e333a 3665
c6f5c050
MYK
3666 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3667 if (hsotg->eps_in[ep])
1f91b4cc 3668 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
c6f5c050 3669 if (hsotg->eps_out[ep])
1f91b4cc 3670 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
c6f5c050 3671 }
b83e333a
MS
3672 }
3673
09a75e85 3674 return 0;
b83e333a
MS
3675}
3676
1f91b4cc 3677int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
b83e333a 3678{
b83e333a 3679 unsigned long flags;
b83e333a 3680
9e779778 3681 if (hsotg->lx_state == DWC2_L2)
09a75e85 3682 return 0;
9e779778 3683
b83e333a
MS
3684 if (hsotg->driver) {
3685 dev_info(hsotg->dev, "resuming usb gadget %s\n",
3686 hsotg->driver->driver.name);
d00b4142 3687
dc6e69e6 3688 spin_lock_irqsave(&hsotg->lock, flags);
1f91b4cc 3689 dwc2_hsotg_core_init_disconnected(hsotg, false);
dc6e69e6 3690 if (hsotg->enabled)
1f91b4cc 3691 dwc2_hsotg_core_connect(hsotg);
dc6e69e6
MS
3692 spin_unlock_irqrestore(&hsotg->lock, flags);
3693 }
b83e333a 3694
09a75e85 3695 return 0;
b83e333a 3696}
58e52ff6
JY
3697
3698/**
3699 * dwc2_backup_device_registers() - Backup controller device registers.
3700 * When suspending usb bus, registers needs to be backuped
3701 * if controller power is disabled once suspended.
3702 *
3703 * @hsotg: Programming view of the DWC_otg controller
3704 */
3705int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
3706{
3707 struct dwc2_dregs_backup *dr;
3708 int i;
3709
3710 dev_dbg(hsotg->dev, "%s\n", __func__);
3711
3712 /* Backup dev regs */
3713 dr = &hsotg->dr_backup;
3714
3715 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
3716 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
3717 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3718 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
3719 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
3720
3721 for (i = 0; i < hsotg->num_of_eps; i++) {
3722 /* Backup IN EPs */
3723 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
3724
3725 /* Ensure DATA PID is correctly configured */
3726 if (dr->diepctl[i] & DXEPCTL_DPID)
3727 dr->diepctl[i] |= DXEPCTL_SETD1PID;
3728 else
3729 dr->diepctl[i] |= DXEPCTL_SETD0PID;
3730
3731 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
3732 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
3733
3734 /* Backup OUT EPs */
3735 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
3736
3737 /* Ensure DATA PID is correctly configured */
3738 if (dr->doepctl[i] & DXEPCTL_DPID)
3739 dr->doepctl[i] |= DXEPCTL_SETD1PID;
3740 else
3741 dr->doepctl[i] |= DXEPCTL_SETD0PID;
3742
3743 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
3744 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
3745 }
3746 dr->valid = true;
3747 return 0;
3748}
3749
3750/**
3751 * dwc2_restore_device_registers() - Restore controller device registers.
3752 * When resuming usb bus, device registers needs to be restored
3753 * if controller power were disabled.
3754 *
3755 * @hsotg: Programming view of the DWC_otg controller
3756 */
3757int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
3758{
3759 struct dwc2_dregs_backup *dr;
3760 u32 dctl;
3761 int i;
3762
3763 dev_dbg(hsotg->dev, "%s\n", __func__);
3764
3765 /* Restore dev regs */
3766 dr = &hsotg->dr_backup;
3767 if (!dr->valid) {
3768 dev_err(hsotg->dev, "%s: no device registers to restore\n",
3769 __func__);
3770 return -EINVAL;
3771 }
3772 dr->valid = false;
3773
3774 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
3775 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
3776 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
3777 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
3778 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
3779
3780 for (i = 0; i < hsotg->num_of_eps; i++) {
3781 /* Restore IN EPs */
3782 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
3783 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
3784 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
3785
3786 /* Restore OUT EPs */
3787 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
3788 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
3789 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
3790 }
3791
3792 /* Set the Power-On Programming done bit */
3793 dctl = dwc2_readl(hsotg->regs + DCTL);
3794 dctl |= DCTL_PWRONPRGDONE;
3795 dwc2_writel(dctl, hsotg->regs + DCTL);
3796
3797 return 0;
3798}