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