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