OSDN Git Service

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