Merge tag 'v3.19-rc5' into next
[deliverable/linux.git] / drivers / usb / dwc2 / gadget.c
index 79242008085bbed84a9e7caf142077215d89aab7..50ae09648cc17befd2f29abeb474dcc2233061c4 100644 (file)
@@ -65,7 +65,16 @@ static inline void __bic32(void __iomem *ptr, u32 val)
        writel(readl(ptr) & ~val, ptr);
 }
 
-/* forward decleration of functions */
+static inline struct s3c_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
+                                               u32 ep_index, u32 dir_in)
+{
+       if (dir_in)
+               return hsotg->eps_in[ep_index];
+       else
+               return hsotg->eps_out[ep_index];
+}
+
+/* forward declaration of functions */
 static void s3c_hsotg_dump(struct dwc2_hsotg *hsotg);
 
 /**
@@ -85,11 +94,11 @@ static void s3c_hsotg_dump(struct dwc2_hsotg *hsotg);
  * a core reset. This means we either need to fix the gadgets to take
  * account of DMA alignment, or add bounce buffers (yuerk).
  *
- * Until this issue is sorted out, we always return 'false'.
+ * g_using_dma is set depending on dts flag.
  */
 static inline bool using_dma(struct dwc2_hsotg *hsotg)
 {
-       return false;   /* support is not complete */
+       return hsotg->g_using_dma;
 }
 
 /**
@@ -165,15 +174,18 @@ static void s3c_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
 {
        unsigned int ep;
        unsigned int addr;
-       unsigned int size;
        int timeout;
        u32 val;
 
-       /* set FIFO sizes to 2048/1024 */
+       /* Reset fifo map if not correctly cleared during previous session */
+       WARN_ON(hsotg->fifo_map);
+       hsotg->fifo_map = 0;
 
-       writel(2048, hsotg->regs + GRXFSIZ);
-       writel((2048 << FIFOSIZE_STARTADDR_SHIFT) |
-               (1024 << FIFOSIZE_DEPTH_SHIFT), hsotg->regs + GNPTXFSIZ);
+       /* set RX/NPTX FIFO sizes */
+       writel(hsotg->g_rx_fifo_sz, hsotg->regs + GRXFSIZ);
+       writel((hsotg->g_rx_fifo_sz << FIFOSIZE_STARTADDR_SHIFT) |
+               (hsotg->g_np_g_tx_fifo_sz << FIFOSIZE_DEPTH_SHIFT),
+               hsotg->regs + GNPTXFSIZ);
 
        /*
         * arange all the rest of the TX FIFOs, as some versions of this
@@ -183,35 +195,21 @@ static void s3c_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
         */
 
        /* start at the end of the GNPTXFSIZ, rounded up */
-       addr = 2048 + 1024;
+       addr = hsotg->g_rx_fifo_sz + hsotg->g_np_g_tx_fifo_sz;
 
        /*
-        * Because we have not enough memory to have each TX FIFO of size at
-        * least 3072 bytes (the maximum single packet size), we create four
-        * FIFOs of lenght 1024, and four of length 3072 bytes, and assing
+        * Configure fifos sizes from provided configuration and assign
         * them to endpoints dynamically according to maxpacket size value of
         * given endpoint.
         */
-
-       /* 256*4=1024 bytes FIFO length */
-       size = 256;
-       for (ep = 1; ep <= 4; ep++) {
-               val = addr;
-               val |= size << FIFOSIZE_DEPTH_SHIFT;
-               WARN_ONCE(addr + size > hsotg->fifo_mem,
-                         "insufficient fifo memory");
-               addr += size;
-
-               writel(val, hsotg->regs + DPTXFSIZN(ep));
-       }
-       /* 768*4=3072 bytes FIFO length */
-       size = 768;
-       for (ep = 5; ep <= 8; ep++) {
+       for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
+               if (!hsotg->g_tx_fifo_sz[ep])
+                       continue;
                val = addr;
-               val |= size << FIFOSIZE_DEPTH_SHIFT;
-               WARN_ONCE(addr + size > hsotg->fifo_mem,
+               val |= hsotg->g_tx_fifo_sz[ep] << FIFOSIZE_DEPTH_SHIFT;
+               WARN_ONCE(addr + hsotg->g_tx_fifo_sz[ep] > hsotg->fifo_mem,
                          "insufficient fifo memory");
-               addr += size;
+               addr += hsotg->g_tx_fifo_sz[ep];
 
                writel(val, hsotg->regs + DPTXFSIZN(ep));
        }
@@ -236,6 +234,7 @@ static void s3c_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
                        dev_err(hsotg->dev,
                                "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
                                __func__, val);
+                       break;
                }
 
                udelay(1);
@@ -604,14 +603,15 @@ static void s3c_hsotg_start_req(struct dwc2_hsotg *hsotg,
        else
                epsize = 0;
 
-       if (index != 0 && ureq->zero) {
-               /*
-                * test for the packets being exactly right for the
-                * transfer
-                */
-
-               if (length == (packets * hs_ep->ep.maxpacket))
-                       packets++;
+       /*
+        * zero length packet should be programmed on its own and should not
+        * be counted in DIEPTSIZ.PktCnt with other packets.
+        */
+       if (dir_in && ureq->zero && !continuing) {
+               /* Test if zlp is actually required. */
+               if ((ureq->length >= hs_ep->ep.maxpacket) &&
+                                       !(ureq->length % hs_ep->ep.maxpacket))
+                       hs_ep->send_zlp = 1;
        }
 
        epsize |= DXEPTSIZ_PKTCNT(packets);
@@ -644,15 +644,12 @@ static void s3c_hsotg_start_req(struct dwc2_hsotg *hsotg,
        ctrl |= DXEPCTL_EPENA;  /* ensure ep enabled */
        ctrl |= DXEPCTL_USBACTEP;
 
-       dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
+       dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
 
        /* For Setup request do not clear NAK */
-       if (hsotg->setup && index == 0)
-               hsotg->setup = 0;
-       else
+       if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
                ctrl |= DXEPCTL_CNAK;   /* clear NAK set by core */
 
-
        dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
        writel(ctrl, hsotg->regs + epctrl_reg);
 
@@ -686,7 +683,7 @@ static void s3c_hsotg_start_req(struct dwc2_hsotg *hsotg,
 
        /* check ep is enabled */
        if (!(readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
-               dev_warn(hsotg->dev,
+               dev_dbg(hsotg->dev,
                         "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
                         index, readl(hsotg->regs + epctrl_reg));
 
@@ -819,7 +816,7 @@ static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
 static struct s3c_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
                                           u32 windex)
 {
-       struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
+       struct s3c_hsotg_ep *ep;
        int dir = (windex & USB_DIR_IN) ? 1 : 0;
        int idx = windex & 0x7F;
 
@@ -829,6 +826,8 @@ static struct s3c_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
        if (idx > hsotg->num_of_eps)
                return NULL;
 
+       ep = index_to_ep(hsotg, idx, dir);
+
        if (idx && ep->dir_in != dir)
                return NULL;
 
@@ -864,13 +863,15 @@ static int s3c_hsotg_send_reply(struct dwc2_hsotg *hsotg,
 
        req->buf = hsotg->ep0_buff;
        req->length = length;
-       req->zero = 1; /* always do zero-length final transfer */
+       /*
+        * zero flag is for sending zlp in DATA IN stage. It has no impact on
+        * STATUS stage.
+        */
+       req->zero = 0;
        req->complete = s3c_hsotg_complete_oursetup;
 
        if (length)
                memcpy(req->buf, buff, length);
-       else
-               ep->sent_zlp = 1;
 
        ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
        if (ret) {
@@ -889,7 +890,7 @@ static int s3c_hsotg_send_reply(struct dwc2_hsotg *hsotg,
 static int s3c_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
                                        struct usb_ctrlrequest *ctrl)
 {
-       struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
+       struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
        struct s3c_hsotg_ep *ep;
        __le16 reply;
        int ret;
@@ -960,7 +961,7 @@ static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
 static int s3c_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
                                         struct usb_ctrlrequest *ctrl)
 {
-       struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
+       struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
        struct s3c_hsotg_req *hs_req;
        bool restart;
        bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
@@ -1040,7 +1041,7 @@ static void s3c_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
  */
 static void s3c_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
 {
-       struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
+       struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
        u32 reg;
        u32 ctrl;
 
@@ -1080,34 +1081,29 @@ static void s3c_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
 static void s3c_hsotg_process_control(struct dwc2_hsotg *hsotg,
                                      struct usb_ctrlrequest *ctrl)
 {
-       struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
+       struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
        int ret = 0;
        u32 dcfg;
 
-       ep0->sent_zlp = 0;
-
        dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
                 ctrl->bRequest, ctrl->bRequestType,
                 ctrl->wValue, ctrl->wLength);
 
-       /*
-        * record the direction of the request, for later use when enquing
-        * packets onto EP0.
-        */
-
-       ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
-       dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
-
-       /*
-        * if we've no data with this request, then the last part of the
-        * transaction is going to implicitly be IN.
-        */
-       if (ctrl->wLength == 0)
+       if (ctrl->wLength == 0) {
+               ep0->dir_in = 1;
+               hsotg->ep0_state = DWC2_EP0_STATUS_IN;
+       } else if (ctrl->bRequestType & USB_DIR_IN) {
                ep0->dir_in = 1;
+               hsotg->ep0_state = DWC2_EP0_DATA_IN;
+       } else {
+               ep0->dir_in = 0;
+               hsotg->ep0_state = DWC2_EP0_DATA_OUT;
+       }
 
        if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
                switch (ctrl->bRequest) {
                case USB_REQ_SET_ADDRESS:
+                       hsotg->connected = 1;
                        dcfg = readl(hsotg->regs + DCFG);
                        dcfg &= ~DCFG_DEVADDR_MASK;
                        dcfg |= (le16_to_cpu(ctrl->wValue) <<
@@ -1201,9 +1197,11 @@ static void s3c_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
                return;
        }
 
-       hsotg->eps[0].dir_in = 0;
+       hsotg->eps_out[0]->dir_in = 0;
+       hsotg->eps_out[0]->send_zlp = 0;
+       hsotg->ep0_state = DWC2_EP0_SETUP;
 
-       ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
+       ret = s3c_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
        if (ret < 0) {
                dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
                /*
@@ -1213,6 +1211,27 @@ static void s3c_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
        }
 }
 
+static void s3c_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
+                                       struct s3c_hsotg_ep *hs_ep)
+{
+       u32 ctrl;
+       u8 index = hs_ep->index;
+       u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
+       u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
+
+       dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n", index);
+
+       writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
+                       DXEPTSIZ_XFERSIZE(0), hsotg->regs +
+                       epsiz_reg);
+
+       ctrl = readl(hsotg->regs + epctl_reg);
+       ctrl |= DXEPCTL_CNAK;  /* clear NAK set by core */
+       ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
+       ctrl |= DXEPCTL_USBACTEP;
+       writel(ctrl, hsotg->regs + epctl_reg);
+}
+
 /**
  * s3c_hsotg_complete_request - complete a request given to us
  * @hsotg: The device state.
@@ -1293,7 +1312,7 @@ static void s3c_hsotg_complete_request(struct dwc2_hsotg *hsotg,
  */
 static void s3c_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
 {
-       struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
+       struct s3c_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
        struct s3c_hsotg_req *hs_req = hs_ep->req;
        void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
        int to_read;
@@ -1305,7 +1324,7 @@ static void s3c_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
                u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
                int ptr;
 
-               dev_warn(hsotg->dev,
+               dev_dbg(hsotg->dev,
                         "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
                         __func__, size, ep_idx, epctl);
 
@@ -1345,9 +1364,9 @@ static void s3c_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
 }
 
 /**
- * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
+ * s3c_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
  * @hsotg: The device instance
- * @req: The request currently on this endpoint
+ * @dir_in: If IN zlp
  *
  * Generate a zero-length IN packet request for terminating a SETUP
  * transaction.
@@ -1356,53 +1375,28 @@ static void s3c_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
  * currently believed that we do not need to wait for any space in
  * the TxFIFO.
  */
-static void s3c_hsotg_send_zlp(struct dwc2_hsotg *hsotg,
-                              struct s3c_hsotg_req *req)
+static void s3c_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
 {
-       u32 ctrl;
-
-       if (!req) {
-               dev_warn(hsotg->dev, "%s: no request?\n", __func__);
-               return;
-       }
+       /* eps_out[0] is used in both directions */
+       hsotg->eps_out[0]->dir_in = dir_in;
+       hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
 
-       if (req->req.length == 0) {
-               hsotg->eps[0].sent_zlp = 1;
-               s3c_hsotg_enqueue_setup(hsotg);
-               return;
-       }
-
-       hsotg->eps[0].dir_in = 1;
-       hsotg->eps[0].sent_zlp = 1;
-
-       dev_dbg(hsotg->dev, "sending zero-length packet\n");
-
-       /* issue a zero-sized packet to terminate this */
-       writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
-              DXEPTSIZ_XFERSIZE(0), hsotg->regs + DIEPTSIZ(0));
-
-       ctrl = readl(hsotg->regs + DIEPCTL0);
-       ctrl |= DXEPCTL_CNAK;  /* clear NAK set by core */
-       ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
-       ctrl |= DXEPCTL_USBACTEP;
-       writel(ctrl, hsotg->regs + DIEPCTL0);
+       s3c_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
 }
 
 /**
  * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
  * @hsotg: The device instance
  * @epnum: The endpoint received from
- * @was_setup: Set if processing a SetupDone event.
  *
  * The RXFIFO has delivered an OutDone event, which means that the data
  * transfer for an OUT endpoint has been completed, either by a short
  * packet or by the finish of a transfer.
  */
-static void s3c_hsotg_handle_outdone(struct dwc2_hsotg *hsotg,
-                                    int epnum, bool was_setup)
+static void s3c_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
 {
        u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
-       struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
+       struct s3c_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
        struct s3c_hsotg_req *hs_req = hs_ep->req;
        struct usb_request *req = &hs_req->req;
        unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
@@ -1413,6 +1407,13 @@ static void s3c_hsotg_handle_outdone(struct dwc2_hsotg *hsotg,
                return;
        }
 
+       if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
+               dev_dbg(hsotg->dev, "zlp packet received\n");
+               s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
+               s3c_hsotg_enqueue_setup(hsotg);
+               return;
+       }
+
        if (using_dma(hsotg)) {
                unsigned size_done;
 
@@ -1435,12 +1436,6 @@ static void s3c_hsotg_handle_outdone(struct dwc2_hsotg *hsotg,
        if (req->actual < req->length && size_left == 0) {
                s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
                return;
-       } else if (epnum == 0) {
-               /*
-                * After was_setup = 1 =>
-                * set CNAK for non Setup requests
-                */
-               hsotg->setup = was_setup ? 0 : 1;
        }
 
        if (req->actual < req->length && req->short_not_ok) {
@@ -1453,13 +1448,10 @@ static void s3c_hsotg_handle_outdone(struct dwc2_hsotg *hsotg,
                 */
        }
 
-       if (epnum == 0) {
-               /*
-                * Condition req->complete != s3c_hsotg_complete_setup says:
-                * send ZLP when we have an asynchronous request from gadget
-                */
-               if (!was_setup && req->complete != s3c_hsotg_complete_setup)
-                       s3c_hsotg_send_zlp(hsotg, hs_req);
+       if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
+               /* Move to STATUS IN */
+               s3c_hsotg_ep0_zlp(hsotg, true);
+               return;
        }
 
        s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
@@ -1525,7 +1517,7 @@ static void s3c_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
                        s3c_hsotg_read_frameno(hsotg));
 
                if (!using_dma(hsotg))
-                       s3c_hsotg_handle_outdone(hsotg, epnum, false);
+                       s3c_hsotg_handle_outdone(hsotg, epnum);
                break;
 
        case GRXSTS_PKTSTS_SETUPDONE:
@@ -1533,8 +1525,13 @@ static void s3c_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
                        "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
                        s3c_hsotg_read_frameno(hsotg),
                        readl(hsotg->regs + DOEPCTL(0)));
-
-               s3c_hsotg_handle_outdone(hsotg, epnum, true);
+               /*
+                * Call s3c_hsotg_handle_outdone here if it was not called from
+                * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
+                * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
+                */
+               if (hsotg->ep0_state == DWC2_EP0_SETUP)
+                       s3c_hsotg_handle_outdone(hsotg, epnum);
                break;
 
        case GRXSTS_PKTSTS_OUTRX:
@@ -1547,6 +1544,8 @@ static void s3c_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
                        s3c_hsotg_read_frameno(hsotg),
                        readl(hsotg->regs + DOEPCTL(0)));
 
+               WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
+
                s3c_hsotg_rx_data(hsotg, epnum, size);
                break;
 
@@ -1591,14 +1590,18 @@ static u32 s3c_hsotg_ep0_mps(unsigned int mps)
  * the hardware control registers to reflect this.
  */
 static void s3c_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
-                                      unsigned int ep, unsigned int mps)
+                       unsigned int ep, unsigned int mps, unsigned int dir_in)
 {
-       struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
+       struct s3c_hsotg_ep *hs_ep;
        void __iomem *regs = hsotg->regs;
        u32 mpsval;
        u32 mcval;
        u32 reg;
 
+       hs_ep = index_to_ep(hsotg, ep, dir_in);
+       if (!hs_ep)
+               return;
+
        if (ep == 0) {
                /* EP0 is a special case */
                mpsval = s3c_hsotg_ep0_mps(mps);
@@ -1617,17 +1620,12 @@ static void s3c_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
                hs_ep->ep.maxpacket = mpsval;
        }
 
-       /*
-        * update both the in and out endpoint controldir_ registers, even
-        * if one of the directions may not be in use.
-        */
-
-       reg = readl(regs + DIEPCTL(ep));
-       reg &= ~DXEPCTL_MPS_MASK;
-       reg |= mpsval;
-       writel(reg, regs + DIEPCTL(ep));
-
-       if (ep) {
+       if (dir_in) {
+               reg = readl(regs + DIEPCTL(ep));
+               reg &= ~DXEPCTL_MPS_MASK;
+               reg |= mpsval;
+               writel(reg, regs + DIEPCTL(ep));
+       } else {
                reg = readl(regs + DOEPCTL(ep));
                reg &= ~DXEPCTL_MPS_MASK;
                reg |= mpsval;
@@ -1727,9 +1725,10 @@ static void s3c_hsotg_complete_in(struct dwc2_hsotg *hsotg,
        }
 
        /* Finish ZLP handling for IN EP0 transactions */
-       if (hsotg->eps[0].sent_zlp) {
-               dev_dbg(hsotg->dev, "zlp packet received\n");
+       if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
+               dev_dbg(hsotg->dev, "zlp packet sent\n");
                s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
+               s3c_hsotg_enqueue_setup(hsotg);
                return;
        }
 
@@ -1756,31 +1755,27 @@ static void s3c_hsotg_complete_in(struct dwc2_hsotg *hsotg,
        dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
                hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
 
-       /*
-        * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0
-        * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B
-        * ,256B ... ), after last MPS sized packet send IN ZLP packet to
-        * inform the host that no more data is available.
-        * The state of req.zero member is checked to be sure that the value to
-        * send is smaller than wValue expected from host.
-        * Check req.length to NOT send another ZLP when the current one is
-        * under completion (the one for which this completion has been called).
-        */
-       if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
-           hs_req->req.length == hs_req->req.actual &&
-           !(hs_req->req.length % hs_ep->ep.maxpacket)) {
+       if (!size_left && hs_req->req.actual < hs_req->req.length) {
+               dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
+               s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
+               return;
+       }
 
-               dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
-               s3c_hsotg_send_zlp(hsotg, hs_req);
+       /* Zlp for all endpoints, for ep0 only in DATA IN stage */
+       if (hs_ep->send_zlp) {
+               s3c_hsotg_program_zlp(hsotg, hs_ep);
+               hs_ep->send_zlp = 0;
+               /* transfer will be completed on next complete interrupt */
+               return;
+       }
 
+       if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
+               /* Move to STATUS OUT */
+               s3c_hsotg_ep0_zlp(hsotg, false);
                return;
        }
 
-       if (!size_left && hs_req->req.actual < hs_req->req.length) {
-               dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
-               s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
-       } else
-               s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
+       s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
 }
 
 /**
@@ -1794,7 +1789,7 @@ static void s3c_hsotg_complete_in(struct dwc2_hsotg *hsotg,
 static void s3c_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
                            int dir_in)
 {
-       struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
+       struct s3c_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
        u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
        u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
        u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
@@ -1807,9 +1802,19 @@ static void s3c_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
        /* Clear endpoint interrupts */
        writel(ints, hsotg->regs + epint_reg);
 
+       if (!hs_ep) {
+               dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
+                                       __func__, idx, dir_in ? "in" : "out");
+               return;
+       }
+
        dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
                __func__, idx, dir_in ? "in" : "out", ints);
 
+       /* Don't process XferCompl interrupt if it is a setup packet */
+       if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
+               ints &= ~DXEPINT_XFERCOMPL;
+
        if (ints & DXEPINT_XFERCOMPL) {
                if (hs_ep->isochronous && hs_ep->interval == 1) {
                        if (ctrl & DXEPCTL_EOFRNUM)
@@ -1839,7 +1844,7 @@ static void s3c_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
                         * as we ignore the RXFIFO.
                         */
 
-                       s3c_hsotg_handle_outdone(hsotg, idx, false);
+                       s3c_hsotg_handle_outdone(hsotg, idx);
                }
        }
 
@@ -1878,7 +1883,7 @@ static void s3c_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
                        if (dir_in)
                                WARN_ON_ONCE(1);
                        else
-                               s3c_hsotg_handle_outdone(hsotg, 0, true);
+                               s3c_hsotg_handle_outdone(hsotg, 0);
                }
        }
 
@@ -1969,9 +1974,15 @@ static void s3c_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
 
        if (ep0_mps) {
                int i;
-               s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
-               for (i = 1; i < hsotg->num_of_eps; i++)
-                       s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
+               /* Initialize ep0 for both in and out directions */
+               s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 1);
+               s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0);
+               for (i = 1; i < hsotg->num_of_eps; i++) {
+                       if (hsotg->eps_in[i])
+                               s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 1);
+                       if (hsotg->eps_out[i])
+                               s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 0);
+               }
        }
 
        /* ensure after enumeration our EP0 is active */
@@ -1988,30 +1999,23 @@ static void s3c_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
  * @hsotg: The device state.
  * @ep: The endpoint the requests may be on.
  * @result: The result code to use.
- * @force: Force removal of any current requests
  *
  * Go through the requests on the given endpoint and mark them
  * completed with the given result code.
  */
 static void kill_all_requests(struct dwc2_hsotg *hsotg,
                              struct s3c_hsotg_ep *ep,
-                             int result, bool force)
+                             int result)
 {
        struct s3c_hsotg_req *req, *treq;
        unsigned size;
 
-       list_for_each_entry_safe(req, treq, &ep->queue, queue) {
-               /*
-                * currently, we can't do much about an already
-                * running request on an in endpoint
-                */
-
-               if (ep->req == req && ep->dir_in && !force)
-                       continue;
+       ep->req = NULL;
 
+       list_for_each_entry_safe(req, treq, &ep->queue, queue)
                s3c_hsotg_complete_request(hsotg, ep, req,
                                           result);
-       }
+
        if (!hsotg->dedicated_fifos)
                return;
        size = (readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4;
@@ -2035,8 +2039,15 @@ void s3c_hsotg_disconnect(struct dwc2_hsotg *hsotg)
                return;
 
        hsotg->connected = 0;
-       for (ep = 0; ep < hsotg->num_of_eps; ep++)
-               kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
+
+       for (ep = 0; ep < hsotg->num_of_eps; ep++) {
+               if (hsotg->eps_in[ep])
+                       kill_all_requests(hsotg, hsotg->eps_in[ep],
+                                                               -ESHUTDOWN);
+               if (hsotg->eps_out[ep])
+                       kill_all_requests(hsotg, hsotg->eps_out[ep],
+                                                               -ESHUTDOWN);
+       }
 
        call_gadget(hsotg, disconnect);
 }
@@ -2053,9 +2064,11 @@ static void s3c_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
        int epno, ret;
 
        /* look through for any more data to transmit */
-
        for (epno = 0; epno < hsotg->num_of_eps; epno++) {
-               ep = &hsotg->eps[epno];
+               ep = index_to_ep(hsotg, epno, 1);
+
+               if (!ep)
+                       continue;
 
                if (!ep->dir_in)
                        continue;
@@ -2163,7 +2176,7 @@ void s3c_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg)
 
        if (using_dma(hsotg))
                writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
-                      GAHBCFG_HBSTLEN_INCR4,
+                      (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
                       hsotg->regs + GAHBCFG);
        else
                writel(((hsotg->dedicated_fifos) ? (GAHBCFG_NP_TXF_EMP_LVL |
@@ -2177,8 +2190,8 @@ void s3c_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg)
         * interrupts.
         */
 
-       writel(((hsotg->dedicated_fifos) ? DIEPMSK_TXFIFOEMPTY |
-               DIEPMSK_INTKNTXFEMPMSK : 0) |
+       writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
+               DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
                DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
                DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
                DIEPMSK_INTKNEPMISMSK,
@@ -2230,13 +2243,13 @@ void s3c_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg)
        writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
               DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
 
-       writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
+       writel(s3c_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
               DXEPCTL_CNAK | DXEPCTL_EPENA |
               DXEPCTL_USBACTEP,
               hsotg->regs + DOEPCTL0);
 
        /* enable, but don't activate EP0in */
-       writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
+       writel(s3c_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
               DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
 
        s3c_hsotg_enqueue_setup(hsotg);
@@ -2293,7 +2306,6 @@ irq_retry:
                writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
 
                s3c_hsotg_irq_enumdone(hsotg);
-               hsotg->connected = 1;
        }
 
        if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
@@ -2308,12 +2320,14 @@ irq_retry:
 
                dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
 
-               for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
+               for (ep = 0; ep < hsotg->num_of_eps && daint_out;
+                                               ep++, daint_out >>= 1) {
                        if (daint_out & 1)
                                s3c_hsotg_epint(hsotg, ep, 0);
                }
 
-               for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
+               for (ep = 0; ep < hsotg->num_of_eps  && daint_in;
+                                               ep++, daint_in >>= 1) {
                        if (daint_in & 1)
                                s3c_hsotg_epint(hsotg, ep, 1);
                }
@@ -2329,12 +2343,15 @@ irq_retry:
 
                writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
 
+               /* Report disconnection if it is not already done. */
+               s3c_hsotg_disconnect(hsotg);
+
                if (usb_status & GOTGCTL_BSESVLD) {
                        if (time_after(jiffies, hsotg->last_rst +
                                       msecs_to_jiffies(200))) {
 
-                               kill_all_requests(hsotg, &hsotg->eps[0],
-                                                         -ECONNRESET, true);
+                               kill_all_requests(hsotg, hsotg->eps_out[0],
+                                                         -ECONNRESET);
 
                                s3c_hsotg_core_init_disconnected(hsotg);
                                s3c_hsotg_core_connect(hsotg);
@@ -2429,12 +2446,12 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
        struct s3c_hsotg_ep *hs_ep = our_ep(ep);
        struct dwc2_hsotg *hsotg = hs_ep->parent;
        unsigned long flags;
-       int index = hs_ep->index;
+       unsigned int index = hs_ep->index;
        u32 epctrl_reg;
        u32 epctrl;
        u32 mps;
-       int dir_in;
-       int i, val, size;
+       unsigned int dir_in;
+       unsigned int i, val, size;
        int ret = 0;
 
        dev_dbg(hsotg->dev,
@@ -2482,7 +2499,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
        epctrl |= DXEPCTL_SNAK;
 
        /* update the endpoint state */
-       s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps);
+       s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, dir_in);
 
        /* default, set to non-periodic */
        hs_ep->isochronous = 0;
@@ -2518,30 +2535,48 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
                break;
        }
 
+       /* If fifo is already allocated for this ep */
+       if (hs_ep->fifo_index) {
+               size =  hs_ep->ep.maxpacket * hs_ep->mc;
+               /* If bigger fifo is required deallocate current one */
+               if (size > hs_ep->fifo_size) {
+                       hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
+                       hs_ep->fifo_index = 0;
+                       hs_ep->fifo_size = 0;
+               }
+       }
+
        /*
         * if the hardware has dedicated fifos, we must give each IN EP
         * a unique tx-fifo even if it is non-periodic.
         */
-       if (dir_in && hsotg->dedicated_fifos) {
+       if (dir_in && hsotg->dedicated_fifos && !hs_ep->fifo_index) {
+               u32 fifo_index = 0;
+               u32 fifo_size = UINT_MAX;
                size = hs_ep->ep.maxpacket*hs_ep->mc;
-               for (i = 1; i <= 8; ++i) {
+               for (i = 1; i < hsotg->num_of_eps; ++i) {
                        if (hsotg->fifo_map & (1<<i))
                                continue;
                        val = readl(hsotg->regs + DPTXFSIZN(i));
                        val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
                        if (val < size)
                                continue;
-                       hsotg->fifo_map |= 1<<i;
-
-                       epctrl |= DXEPCTL_TXFNUM(i);
-                       hs_ep->fifo_index = i;
-                       hs_ep->fifo_size = val;
-                       break;
+                       /* Search for smallest acceptable fifo */
+                       if (val < fifo_size) {
+                               fifo_size = val;
+                               fifo_index = i;
+                       }
                }
-               if (i == 8) {
+               if (!fifo_index) {
+                       dev_err(hsotg->dev,
+                               "%s: No suitable fifo found\n", __func__);
                        ret = -ENOMEM;
                        goto error;
                }
+               hsotg->fifo_map |= 1 << fifo_index;
+               epctrl |= DXEPCTL_TXFNUM(fifo_index);
+               hs_ep->fifo_index = fifo_index;
+               hs_ep->fifo_size = fifo_size;
        }
 
        /* for non control endpoints, set PID to D0 */
@@ -2579,7 +2614,7 @@ static int s3c_hsotg_ep_disable_force(struct usb_ep *ep, bool force)
 
        dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
 
-       if (ep == &hsotg->eps[0].ep) {
+       if (ep == &hsotg->eps_out[0]->ep) {
                dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
                return -EINVAL;
        }
@@ -2587,8 +2622,6 @@ static int s3c_hsotg_ep_disable_force(struct usb_ep *ep, bool force)
        epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
 
        spin_lock_irqsave(&hsotg->lock, flags);
-       /* terminate all requests with shutdown */
-       kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, force);
 
        hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
        hs_ep->fifo_index = 0;
@@ -2605,6 +2638,9 @@ static int s3c_hsotg_ep_disable_force(struct usb_ep *ep, bool force)
        /* disable endpoint interrupts */
        s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
 
+       /* terminate all requests with shutdown */
+       kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
+
        spin_unlock_irqrestore(&hsotg->lock, flags);
        return 0;
 }
@@ -2682,40 +2718,39 @@ static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
                return 0;
        }
 
-       /* write both IN and OUT control registers */
-
-       epreg = DIEPCTL(index);
-       epctl = readl(hs->regs + epreg);
-
-       if (value) {
-               epctl |= DXEPCTL_STALL + DXEPCTL_SNAK;
-               if (epctl & DXEPCTL_EPENA)
-                       epctl |= DXEPCTL_EPDIS;
+       if (hs_ep->dir_in) {
+               epreg = DIEPCTL(index);
+               epctl = readl(hs->regs + epreg);
+
+               if (value) {
+                       epctl |= DXEPCTL_STALL + DXEPCTL_SNAK;
+                       if (epctl & DXEPCTL_EPENA)
+                               epctl |= DXEPCTL_EPDIS;
+               } else {
+                       epctl &= ~DXEPCTL_STALL;
+                       xfertype = epctl & DXEPCTL_EPTYPE_MASK;
+                       if (xfertype == DXEPCTL_EPTYPE_BULK ||
+                               xfertype == DXEPCTL_EPTYPE_INTERRUPT)
+                                       epctl |= DXEPCTL_SETD0PID;
+               }
+               writel(epctl, hs->regs + epreg);
        } else {
-               epctl &= ~DXEPCTL_STALL;
-               xfertype = epctl & DXEPCTL_EPTYPE_MASK;
-               if (xfertype == DXEPCTL_EPTYPE_BULK ||
-                       xfertype == DXEPCTL_EPTYPE_INTERRUPT)
-                               epctl |= DXEPCTL_SETD0PID;
-       }
 
-       writel(epctl, hs->regs + epreg);
+               epreg = DOEPCTL(index);
+               epctl = readl(hs->regs + epreg);
 
-       epreg = DOEPCTL(index);
-       epctl = readl(hs->regs + epreg);
-
-       if (value)
-               epctl |= DXEPCTL_STALL;
-       else {
-               epctl &= ~DXEPCTL_STALL;
-               xfertype = epctl & DXEPCTL_EPTYPE_MASK;
-               if (xfertype == DXEPCTL_EPTYPE_BULK ||
-                       xfertype == DXEPCTL_EPTYPE_INTERRUPT)
-                               epctl |= DXEPCTL_SETD0PID;
+               if (value)
+                       epctl |= DXEPCTL_STALL;
+               else {
+                       epctl &= ~DXEPCTL_STALL;
+                       xfertype = epctl & DXEPCTL_EPTYPE_MASK;
+                       if (xfertype == DXEPCTL_EPTYPE_BULK ||
+                               xfertype == DXEPCTL_EPTYPE_INTERRUPT)
+                                       epctl |= DXEPCTL_SETD0PID;
+               }
+               writel(epctl, hs->regs + epreg);
        }
 
-       writel(epctl, hs->regs + epreg);
-
        hs_ep->halted = value;
 
        return 0;
@@ -2834,8 +2869,8 @@ static void s3c_hsotg_init(struct dwc2_hsotg *hsotg)
        writel(GUSBCFG_PHYIF16 | GUSBCFG_TOUTCAL(7) | (0x5 << 10),
               hsotg->regs + GUSBCFG);
 
-       writel(using_dma(hsotg) ? GAHBCFG_DMA_EN : 0x0,
-              hsotg->regs + GAHBCFG);
+       if (using_dma(hsotg))
+               __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
 }
 
 /**
@@ -2889,6 +2924,8 @@ static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
        }
 
        s3c_hsotg_phy_enable(hsotg);
+       if (!IS_ERR_OR_NULL(hsotg->uphy))
+               otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
 
        spin_lock_irqsave(&hsotg->lock, flags);
        s3c_hsotg_init(hsotg);
@@ -2927,8 +2964,12 @@ static int s3c_hsotg_udc_stop(struct usb_gadget *gadget)
        mutex_lock(&hsotg->init_mutex);
 
        /* all endpoints should be shutdown */
-       for (ep = 1; ep < hsotg->num_of_eps; ep++)
-               s3c_hsotg_ep_disable_force(&hsotg->eps[ep].ep, true);
+       for (ep = 1; ep < hsotg->num_of_eps; ep++) {
+               if (hsotg->eps_in[ep])
+                       s3c_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
+               if (hsotg->eps_out[ep])
+                       s3c_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
+       }
 
        spin_lock_irqsave(&hsotg->lock, flags);
 
@@ -2938,6 +2979,8 @@ static int s3c_hsotg_udc_stop(struct usb_gadget *gadget)
 
        spin_unlock_irqrestore(&hsotg->lock, flags);
 
+       if (!IS_ERR_OR_NULL(hsotg->uphy))
+               otg_set_peripheral(hsotg->uphy->otg, NULL);
        s3c_hsotg_phy_disable(hsotg);
 
        regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
@@ -2982,6 +3025,7 @@ static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
                s3c_hsotg_core_connect(hsotg);
        } else {
                s3c_hsotg_core_disconnect(hsotg);
+               s3c_hsotg_disconnect(hsotg);
                hsotg->enabled = 0;
                clk_disable(hsotg->clk);
        }
@@ -2993,11 +3037,52 @@ static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
        return 0;
 }
 
+static int s3c_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
+{
+       struct dwc2_hsotg *hsotg = to_hsotg(gadget);
+       unsigned long flags;
+
+       dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
+       spin_lock_irqsave(&hsotg->lock, flags);
+
+       if (is_active) {
+               /* Kill any ep0 requests as controller will be reinitialized */
+               kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
+               s3c_hsotg_core_init_disconnected(hsotg);
+               if (hsotg->enabled)
+                       s3c_hsotg_core_connect(hsotg);
+       } else {
+               s3c_hsotg_core_disconnect(hsotg);
+               s3c_hsotg_disconnect(hsotg);
+       }
+
+       spin_unlock_irqrestore(&hsotg->lock, flags);
+       return 0;
+}
+
+/**
+ * s3c_hsotg_vbus_draw - report bMaxPower field
+ * @gadget: The usb gadget state
+ * @mA: Amount of current
+ *
+ * Report how much power the device may consume to the phy.
+ */
+static int s3c_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
+{
+       struct dwc2_hsotg *hsotg = to_hsotg(gadget);
+
+       if (IS_ERR_OR_NULL(hsotg->uphy))
+               return -ENOTSUPP;
+       return usb_phy_set_power(hsotg->uphy, mA);
+}
+
 static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
        .get_frame      = s3c_hsotg_gadget_getframe,
        .udc_start              = s3c_hsotg_udc_start,
        .udc_stop               = s3c_hsotg_udc_stop,
        .pullup                 = s3c_hsotg_pullup,
+       .vbus_session           = s3c_hsotg_vbus_session,
+       .vbus_draw              = s3c_hsotg_vbus_draw,
 };
 
 /**
@@ -3012,19 +3097,19 @@ static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
  */
 static void s3c_hsotg_initep(struct dwc2_hsotg *hsotg,
                                       struct s3c_hsotg_ep *hs_ep,
-                                      int epnum)
+                                      int epnum,
+                                      bool dir_in)
 {
        char *dir;
 
        if (epnum == 0)
                dir = "";
-       else if ((epnum % 2) == 0) {
-               dir = "out";
-       } else {
+       else if (dir_in)
                dir = "in";
-               hs_ep->dir_in = 1;
-       }
+       else
+               dir = "out";
 
+       hs_ep->dir_in = dir_in;
        hs_ep->index = epnum;
 
        snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
@@ -3048,8 +3133,10 @@ static void s3c_hsotg_initep(struct dwc2_hsotg *hsotg,
 
        if (using_dma(hsotg)) {
                u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
-               writel(next, hsotg->regs + DIEPCTL(epnum));
-               writel(next, hsotg->regs + DOEPCTL(epnum));
+               if (dir_in)
+                       writel(next, hsotg->regs + DIEPCTL(epnum));
+               else
+                       writel(next, hsotg->regs + DOEPCTL(epnum));
        }
 }
 
@@ -3059,24 +3146,56 @@ static void s3c_hsotg_initep(struct dwc2_hsotg *hsotg,
  *
  * Read the USB core HW configuration registers
  */
-static void s3c_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
+static int s3c_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
 {
-       u32 cfg2, cfg3, cfg4;
+       u32 cfg;
+       u32 ep_type;
+       u32 i;
+
        /* check hardware configuration */
 
-       cfg2 = readl(hsotg->regs + 0x48);
-       hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
+       cfg = readl(hsotg->regs + GHWCFG2);
+       hsotg->num_of_eps = (cfg >> 10) & 0xF;
+       /* Add ep0 */
+       hsotg->num_of_eps++;
 
-       cfg3 = readl(hsotg->regs + 0x4C);
-       hsotg->fifo_mem = (cfg3 >> 16);
+       hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct s3c_hsotg_ep),
+                                                               GFP_KERNEL);
+       if (!hsotg->eps_in[0])
+               return -ENOMEM;
+       /* Same s3c_hsotg_ep is used in both directions for ep0 */
+       hsotg->eps_out[0] = hsotg->eps_in[0];
+
+       cfg = readl(hsotg->regs + GHWCFG1);
+       for (i = 1; i < hsotg->num_of_eps; i++, cfg >>= 2) {
+               ep_type = cfg & 3;
+               /* Direction in or both */
+               if (!(ep_type & 2)) {
+                       hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
+                               sizeof(struct s3c_hsotg_ep), GFP_KERNEL);
+                       if (!hsotg->eps_in[i])
+                               return -ENOMEM;
+               }
+               /* Direction out or both */
+               if (!(ep_type & 1)) {
+                       hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
+                               sizeof(struct s3c_hsotg_ep), GFP_KERNEL);
+                       if (!hsotg->eps_out[i])
+                               return -ENOMEM;
+               }
+       }
+
+       cfg = readl(hsotg->regs + GHWCFG3);
+       hsotg->fifo_mem = (cfg >> 16);
 
-       cfg4 = readl(hsotg->regs + 0x50);
-       hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
+       cfg = readl(hsotg->regs + GHWCFG4);
+       hsotg->dedicated_fifos = (cfg >> 25) & 1;
 
        dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
                 hsotg->num_of_eps,
                 hsotg->dedicated_fifos ? "dedicated" : "shared",
                 hsotg->fifo_mem);
+       return 0;
 }
 
 /**
@@ -3103,14 +3222,14 @@ static void s3c_hsotg_dump(struct dwc2_hsotg *hsotg)
 
        /* show periodic fifo settings */
 
-       for (idx = 1; idx <= 15; idx++) {
+       for (idx = 1; idx < hsotg->num_of_eps; idx++) {
                val = readl(regs + DPTXFSIZN(idx));
                dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
                         val >> FIFOSIZE_DEPTH_SHIFT,
                         val & FIFOSIZE_STARTADDR_MASK);
        }
 
-       for (idx = 0; idx < 15; idx++) {
+       for (idx = 0; idx < hsotg->num_of_eps; idx++) {
                dev_info(dev,
                         "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
                         readl(regs + DIEPCTL(idx)),
@@ -3168,7 +3287,7 @@ static int state_show(struct seq_file *seq, void *v)
 
        seq_puts(seq, "\nEndpoint status:\n");
 
-       for (idx = 0; idx < 15; idx++) {
+       for (idx = 0; idx < hsotg->num_of_eps; idx++) {
                u32 in, out;
 
                in = readl(regs + DIEPCTL(idx));
@@ -3227,7 +3346,7 @@ static int fifo_show(struct seq_file *seq, void *v)
 
        seq_puts(seq, "\nPeriodic TXFIFOs:\n");
 
-       for (idx = 1; idx <= 15; idx++) {
+       for (idx = 1; idx < hsotg->num_of_eps; idx++) {
                val = readl(regs + DPTXFSIZN(idx));
 
                seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
@@ -3371,17 +3490,33 @@ static void s3c_hsotg_create_debug(struct dwc2_hsotg *hsotg)
        if (IS_ERR(hsotg->debug_fifo))
                dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
 
-       /* create one file for each endpoint */
-
+       /* Create one file for each out endpoint */
        for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
-               struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
+               struct s3c_hsotg_ep *ep;
+
+               ep = hsotg->eps_out[epidx];
+               if (ep) {
+                       ep->debugfs = debugfs_create_file(ep->name, 0444,
+                                                         root, ep, &ep_fops);
+
+                       if (IS_ERR(ep->debugfs))
+                               dev_err(hsotg->dev, "failed to create %s debug file\n",
+                                       ep->name);
+               }
+       }
+       /* Create one file for each in endpoint. EP0 is handled with out eps */
+       for (epidx = 1; epidx < hsotg->num_of_eps; epidx++) {
+               struct s3c_hsotg_ep *ep;
 
-               ep->debugfs = debugfs_create_file(ep->name, 0444,
-                                                 root, ep, &ep_fops);
+               ep = hsotg->eps_in[epidx];
+               if (ep) {
+                       ep->debugfs = debugfs_create_file(ep->name, 0444,
+                                                         root, ep, &ep_fops);
 
-               if (IS_ERR(ep->debugfs))
-                       dev_err(hsotg->dev, "failed to create %s debug file\n",
-                               ep->name);
+                       if (IS_ERR(ep->debugfs))
+                               dev_err(hsotg->dev, "failed to create %s debug file\n",
+                                       ep->name);
+               }
        }
 }
 
@@ -3396,8 +3531,10 @@ static void s3c_hsotg_delete_debug(struct dwc2_hsotg *hsotg)
        unsigned epidx;
 
        for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
-               struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
-               debugfs_remove(ep->debugfs);
+               if (hsotg->eps_in[epidx])
+                       debugfs_remove(hsotg->eps_in[epidx]->debugfs);
+               if (hsotg->eps_out[epidx])
+                       debugfs_remove(hsotg->eps_out[epidx]->debugfs);
        }
 
        debugfs_remove(hsotg->debug_file);
@@ -3405,6 +3542,51 @@ static void s3c_hsotg_delete_debug(struct dwc2_hsotg *hsotg)
        debugfs_remove(hsotg->debug_root);
 }
 
+#ifdef CONFIG_OF
+static void s3c_hsotg_of_probe(struct dwc2_hsotg *hsotg)
+{
+       struct device_node *np = hsotg->dev->of_node;
+       u32 len = 0;
+       u32 i = 0;
+
+       /* Enable dma if requested in device tree */
+       hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma");
+
+       /*
+       * Register TX periodic fifo size per endpoint.
+       * EP0 is excluded since it has no fifo configuration.
+       */
+       if (!of_find_property(np, "g-tx-fifo-size", &len))
+               goto rx_fifo;
+
+       len /= sizeof(u32);
+
+       /* Read tx fifo sizes other than ep0 */
+       if (of_property_read_u32_array(np, "g-tx-fifo-size",
+                                               &hsotg->g_tx_fifo_sz[1], len))
+               goto rx_fifo;
+
+       /* Add ep0 */
+       len++;
+
+       /* Make remaining TX fifos unavailable */
+       if (len < MAX_EPS_CHANNELS) {
+               for (i = len; i < MAX_EPS_CHANNELS; i++)
+                       hsotg->g_tx_fifo_sz[i] = 0;
+       }
+
+rx_fifo:
+       /* Register RX fifo size */
+       of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz);
+
+       /* Register NPTX fifo size */
+       of_property_read_u32(np, "g-np-tx-fifo-size",
+                                               &hsotg->g_np_g_tx_fifo_sz);
+}
+#else
+static inline void s3c_hsotg_of_probe(struct dwc2_hsotg *hsotg) { }
+#endif
+
 /**
  * dwc2_gadget_init - init function for gadget
  * @dwc2: The data structure for the DWC2 driver.
@@ -3414,41 +3596,47 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
 {
        struct device *dev = hsotg->dev;
        struct s3c_hsotg_plat *plat = dev->platform_data;
-       struct phy *phy;
-       struct usb_phy *uphy;
-       struct s3c_hsotg_ep *eps;
        int epnum;
        int ret;
        int i;
+       u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE;
 
        /* Set default UTMI width */
        hsotg->phyif = GUSBCFG_PHYIF16;
 
+       s3c_hsotg_of_probe(hsotg);
+
+       /* Initialize to legacy fifo configuration values */
+       hsotg->g_rx_fifo_sz = 2048;
+       hsotg->g_np_g_tx_fifo_sz = 1024;
+       memcpy(&hsotg->g_tx_fifo_sz[1], p_tx_fifo, sizeof(p_tx_fifo));
+       /* Device tree specific probe */
+       s3c_hsotg_of_probe(hsotg);
+       /* Dump fifo information */
+       dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
+                                               hsotg->g_np_g_tx_fifo_sz);
+       dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz);
+       for (i = 0; i < MAX_EPS_CHANNELS; i++)
+               dev_dbg(dev, "Periodic TXFIFO%2d size: %d\n", i,
+                                               hsotg->g_tx_fifo_sz[i]);
        /*
-        * Attempt to find a generic PHY, then look for an old style
-        * USB PHY, finally fall back to pdata
+        * If platform probe couldn't find a generic PHY or an old style
+        * USB PHY, fall back to pdata
         */
-       phy = devm_phy_get(dev, "usb2-phy");
-       if (IS_ERR(phy)) {
-               uphy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
-               if (IS_ERR(uphy)) {
-                       /* Fallback for pdata */
-                       plat = dev_get_platdata(dev);
-                       if (!plat) {
-                               dev_err(dev,
-                               "no platform data or transceiver defined\n");
-                               return -EPROBE_DEFER;
-                       }
-                       hsotg->plat = plat;
-               } else
-                       hsotg->uphy = uphy;
-       } else {
-               hsotg->phy = phy;
+       if (IS_ERR_OR_NULL(hsotg->phy) && IS_ERR_OR_NULL(hsotg->uphy)) {
+               plat = dev_get_platdata(dev);
+               if (!plat) {
+                       dev_err(dev,
+                       "no platform data or transceiver defined\n");
+                       return -EPROBE_DEFER;
+               }
+               hsotg->plat = plat;
+       } else if (hsotg->phy) {
                /*
                 * If using the generic PHY framework, check if the PHY bus
                 * width is 8-bit and set the phyif appropriately.
                 */
-               if (phy_get_bus_width(phy) == 8)
+               if (phy_get_bus_width(hsotg->phy) == 8)
                        hsotg->phyif = GUSBCFG_PHYIF8;
        }
 
@@ -3488,16 +3676,53 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
 
        if (ret) {
                dev_err(dev, "failed to enable supplies: %d\n", ret);
-               goto err_supplies;
+               goto err_clk;
        }
 
        /* usb phy enable */
        s3c_hsotg_phy_enable(hsotg);
 
+       /*
+        * Force Device mode before initialization.
+        * This allows correctly configuring fifo for device mode.
+        */
+       __bic32(hsotg->regs + GUSBCFG, GUSBCFG_FORCEHOSTMODE);
+       __orr32(hsotg->regs + GUSBCFG, GUSBCFG_FORCEDEVMODE);
+
+       /*
+        * According to Synopsys databook, this sleep is needed for the force
+        * device mode to take effect.
+        */
+       msleep(25);
+
        s3c_hsotg_corereset(hsotg);
-       s3c_hsotg_hw_cfg(hsotg);
+       ret = s3c_hsotg_hw_cfg(hsotg);
+       if (ret) {
+               dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
+               goto err_clk;
+       }
+
        s3c_hsotg_init(hsotg);
 
+       /* Switch back to default configuration */
+       __bic32(hsotg->regs + GUSBCFG, GUSBCFG_FORCEDEVMODE);
+
+       hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
+                       DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
+       if (!hsotg->ctrl_buff) {
+               dev_err(dev, "failed to allocate ctrl request buff\n");
+               ret = -ENOMEM;
+               goto err_supplies;
+       }
+
+       hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
+                       DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
+       if (!hsotg->ep0_buff) {
+               dev_err(dev, "failed to allocate ctrl reply buff\n");
+               ret = -ENOMEM;
+               goto err_supplies;
+       }
+
        ret = devm_request_irq(hsotg->dev, irq, s3c_hsotg_irq, IRQF_SHARED,
                                dev_name(hsotg->dev), hsotg);
        if (ret < 0) {
@@ -3506,7 +3731,7 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
                regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
                                       hsotg->supplies);
                dev_err(dev, "cannot claim IRQ for gadget\n");
-               goto err_clk;
+               goto err_supplies;
        }
 
        /* hsotg->num_of_eps holds number of EPs other than ep0 */
@@ -3517,33 +3742,30 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
                goto err_supplies;
        }
 
-       eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
-                     GFP_KERNEL);
-       if (!eps) {
-               ret = -ENOMEM;
-               goto err_supplies;
-       }
-
-       hsotg->eps = eps;
-
        /* setup endpoint information */
 
        INIT_LIST_HEAD(&hsotg->gadget.ep_list);
-       hsotg->gadget.ep0 = &hsotg->eps[0].ep;
+       hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
 
        /* allocate EP0 request */
 
-       hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
+       hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
                                                     GFP_KERNEL);
        if (!hsotg->ctrl_req) {
                dev_err(dev, "failed to allocate ctrl req\n");
                ret = -ENOMEM;
-               goto err_ep_mem;
+               goto err_supplies;
        }
 
        /* initialise the endpoints now the core has been initialised */
-       for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
-               s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
+       for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
+               if (hsotg->eps_in[epnum])
+                       s3c_hsotg_initep(hsotg, hsotg->eps_in[epnum],
+                                                               epnum, 1);
+               if (hsotg->eps_out[epnum])
+                       s3c_hsotg_initep(hsotg, hsotg->eps_out[epnum],
+                                                               epnum, 0);
+       }
 
        /* disable power and clock */
        s3c_hsotg_phy_disable(hsotg);
@@ -3552,12 +3774,12 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
                                    hsotg->supplies);
        if (ret) {
                dev_err(dev, "failed to disable supplies: %d\n", ret);
-               goto err_ep_mem;
+               goto err_supplies;
        }
 
        ret = usb_add_gadget_udc(dev, &hsotg->gadget);
        if (ret)
-               goto err_ep_mem;
+               goto err_supplies;
 
        s3c_hsotg_create_debug(hsotg);
 
@@ -3565,8 +3787,6 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
 
        return 0;
 
-err_ep_mem:
-       kfree(eps);
 err_supplies:
        s3c_hsotg_phy_disable(hsotg);
 err_clk:
@@ -3612,8 +3832,12 @@ int s3c_hsotg_suspend(struct dwc2_hsotg *hsotg)
 
                s3c_hsotg_phy_disable(hsotg);
 
-               for (ep = 0; ep < hsotg->num_of_eps; ep++)
-                       s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
+               for (ep = 0; ep < hsotg->num_of_eps; ep++) {
+                       if (hsotg->eps_in[ep])
+                               s3c_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
+                       if (hsotg->eps_out[ep])
+                               s3c_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
+               }
 
                ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
                                             hsotg->supplies);
This page took 0.044092 seconds and 5 git commands to generate.