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