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