Merge branch 'master' of git://git.denx.de/u-boot-arm
[oweals/u-boot.git] / drivers / usb / gadget / s3c_udc_otg_xfer_dma.c
index 255385750ebfb8724bd6993abb5d48fae9f217d4..06dfeed9051ee3f516a67f1667b415b7406ad31d 100644 (file)
  * Marek Szyprowski <m.szyprowski@samsung.com>
  * Lukasz Majewski <l.majewski@samsumg.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 static u8 clear_feature_num;
@@ -42,10 +29,6 @@ static inline void s3c_udc_ep0_zlp(struct s3c_udc *dev)
 {
        u32 ep_ctrl;
 
-       flush_dcache_range((unsigned long) usb_ctrl_dma_addr,
-                          (unsigned long) usb_ctrl_dma_addr
-                          + DMA_BUFFER_SIZE);
-
        writel(usb_ctrl_dma_addr, &reg->in_endp[EP0_CON].diepdma);
        writel(DIEPT_SIZ_PKT_CNT(1), &reg->in_endp[EP0_CON].dieptsiz);
 
@@ -53,7 +36,7 @@ static inline void s3c_udc_ep0_zlp(struct s3c_udc *dev)
        writel(ep_ctrl|DEPCTL_EPENA|DEPCTL_CNAK,
               &reg->in_endp[EP0_CON].diepctl);
 
-       DEBUG_EP0("%s:EP0 ZLP DIEPCTL0 = 0x%x\n",
+       debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DIEPCTL0 = 0x%x\n",
                __func__, readl(&reg->in_endp[EP0_CON].diepctl));
        dev->ep0state = WAIT_FOR_IN_COMPLETE;
 }
@@ -62,11 +45,8 @@ void s3c_udc_pre_setup(void)
 {
        u32 ep_ctrl;
 
-       DEBUG_IN_EP("%s : Prepare Setup packets.\n", __func__);
-
-       invalidate_dcache_range((unsigned long) usb_ctrl_dma_addr,
-                               (unsigned long) usb_ctrl_dma_addr
-                               + DMA_BUFFER_SIZE);
+       debug_cond(DEBUG_IN_EP,
+                  "%s : Prepare Setup packets.\n", __func__);
 
        writel(DOEPT_SIZ_PKT_CNT(1) | sizeof(struct usb_ctrlrequest),
               &reg->out_endp[EP0_CON].doeptsiz);
@@ -75,9 +55,9 @@ void s3c_udc_pre_setup(void)
        ep_ctrl = readl(&reg->out_endp[EP0_CON].doepctl);
        writel(ep_ctrl|DEPCTL_EPENA, &reg->out_endp[EP0_CON].doepctl);
 
-       DEBUG_EP0("%s:EP0 ZLP DIEPCTL0 = 0x%x\n",
+       debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DIEPCTL0 = 0x%x\n",
                __func__, readl(&reg->in_endp[EP0_CON].diepctl));
-       DEBUG_EP0("%s:EP0 ZLP DOEPCTL0 = 0x%x\n",
+       debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DOEPCTL0 = 0x%x\n",
                __func__, readl(&reg->out_endp[EP0_CON].doepctl));
 
 }
@@ -86,16 +66,13 @@ static inline void s3c_ep0_complete_out(void)
 {
        u32 ep_ctrl;
 
-       DEBUG_EP0("%s:EP0 ZLP DIEPCTL0 = 0x%x\n",
+       debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DIEPCTL0 = 0x%x\n",
                __func__, readl(&reg->in_endp[EP0_CON].diepctl));
-       DEBUG_EP0("%s:EP0 ZLP DOEPCTL0 = 0x%x\n",
+       debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DOEPCTL0 = 0x%x\n",
                __func__, readl(&reg->out_endp[EP0_CON].doepctl));
 
-       DEBUG_IN_EP("%s : Prepare Complete Out packet.\n", __func__);
-
-       invalidate_dcache_range((unsigned long) usb_ctrl_dma_addr,
-                               (unsigned long) usb_ctrl_dma_addr
-                               + DMA_BUFFER_SIZE);
+       debug_cond(DEBUG_IN_EP,
+               "%s : Prepare Complete Out packet.\n", __func__);
 
        writel(DOEPT_SIZ_PKT_CNT(1) | sizeof(struct usb_ctrlrequest),
               &reg->out_endp[EP0_CON].doeptsiz);
@@ -105,9 +82,9 @@ static inline void s3c_ep0_complete_out(void)
        writel(ep_ctrl|DEPCTL_EPENA|DEPCTL_CNAK,
               &reg->out_endp[EP0_CON].doepctl);
 
-       DEBUG_EP0("%s:EP0 ZLP DIEPCTL0 = 0x%x\n",
+       debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DIEPCTL0 = 0x%x\n",
                __func__, readl(&reg->in_endp[EP0_CON].diepctl));
-       DEBUG_EP0("%s:EP0 ZLP DOEPCTL0 = 0x%x\n",
+       debug_cond(DEBUG_EP0 != 0, "%s:EP0 ZLP DOEPCTL0 = 0x%x\n",
                __func__, readl(&reg->out_endp[EP0_CON].doepctl));
 
 }
@@ -120,38 +97,33 @@ static int setdma_rx(struct s3c_ep *ep, struct s3c_request *req)
        u32 ep_num = ep_index(ep);
 
        buf = req->req.buf + req->req.actual;
-
-       length = min(req->req.length - req->req.actual, (int)ep->ep.maxpacket);
+       length = min(req->req.length - req->req.actual,
+                    ep_num ? DMA_BUFFER_SIZE : ep->ep.maxpacket);
 
        ep->len = length;
        ep->dma_buf = buf;
 
-       invalidate_dcache_range((unsigned long) ep->dev->dma_buf[ep_num],
-                               (unsigned long) ep->dev->dma_buf[ep_num]
-                               + DMA_BUFFER_SIZE);
-
-       if (length == 0)
+       if (ep_num == EP0_CON || length == 0)
                pktcnt = 1;
        else
                pktcnt = (length - 1)/(ep->ep.maxpacket) + 1;
 
-       pktcnt = 1;
        ctrl =  readl(&reg->out_endp[ep_num].doepctl);
 
-       writel(the_controller->dma_addr[ep_index(ep)+1],
-              &reg->out_endp[ep_num].doepdma);
+       writel((unsigned int) ep->dma_buf, &reg->out_endp[ep_num].doepdma);
        writel(DOEPT_SIZ_PKT_CNT(pktcnt) | DOEPT_SIZ_XFER_SIZE(length),
               &reg->out_endp[ep_num].doeptsiz);
        writel(DEPCTL_EPENA|DEPCTL_CNAK|ctrl, &reg->out_endp[ep_num].doepctl);
 
-       DEBUG_OUT_EP("%s: EP%d RX DMA start : DOEPDMA = 0x%x,"
-                    "DOEPTSIZ = 0x%x, DOEPCTL = 0x%x\n"
-                    "\tbuf = 0x%p, pktcnt = %d, xfersize = %d\n",
-                    __func__, ep_num,
-                    readl(&reg->out_endp[ep_num].doepdma),
-                    readl(&reg->out_endp[ep_num].doeptsiz),
-                    readl(&reg->out_endp[ep_num].doepctl),
-                    buf, pktcnt, length);
+       debug_cond(DEBUG_OUT_EP != 0,
+                  "%s: EP%d RX DMA start : DOEPDMA = 0x%x,"
+                  "DOEPTSIZ = 0x%x, DOEPCTL = 0x%x\n"
+                  "\tbuf = 0x%p, pktcnt = %d, xfersize = %d\n",
+                  __func__, ep_num,
+                  readl(&reg->out_endp[ep_num].doepdma),
+                  readl(&reg->out_endp[ep_num].doeptsiz),
+                  readl(&reg->out_endp[ep_num].doepctl),
+                  buf, pktcnt, length);
        return 0;
 
 }
@@ -161,20 +133,19 @@ int setdma_tx(struct s3c_ep *ep, struct s3c_request *req)
        u32 *buf, ctrl = 0;
        u32 length, pktcnt;
        u32 ep_num = ep_index(ep);
-       u32 *p = the_controller->dma_buf[ep_index(ep)+1];
 
        buf = req->req.buf + req->req.actual;
        length = req->req.length - req->req.actual;
 
        if (ep_num == EP0_CON)
-               length = min_t(length, (u32)ep_maxpacket(ep));
+               length = min(length, (u32)ep_maxpacket(ep));
 
        ep->len = length;
        ep->dma_buf = buf;
-       memcpy(p, ep->dma_buf, length);
 
-       flush_dcache_range((unsigned long) p ,
-                          (unsigned long) p + DMA_BUFFER_SIZE);
+       flush_dcache_range((unsigned long) ep->dma_buf,
+                          (unsigned long) ep->dma_buf +
+                          ROUND(ep->len, CONFIG_SYS_CACHELINE_SIZE));
 
        if (length == 0)
                pktcnt = 1;
@@ -187,8 +158,7 @@ int setdma_tx(struct s3c_ep *ep, struct s3c_request *req)
        while (readl(&reg->grstctl) & TX_FIFO_FLUSH)
                ;
 
-       writel(the_controller->dma_addr[ep_index(ep)+1],
-              &reg->in_endp[ep_num].diepdma);
+       writel((unsigned long) ep->dma_buf, &reg->in_endp[ep_num].diepdma);
        writel(DIEPT_SIZ_PKT_CNT(pktcnt) | DIEPT_SIZ_XFER_SIZE(length),
               &reg->in_endp[ep_num].dieptsiz);
 
@@ -203,14 +173,15 @@ int setdma_tx(struct s3c_ep *ep, struct s3c_request *req)
 
        writel(DEPCTL_EPENA|DEPCTL_CNAK|ctrl, &reg->in_endp[ep_num].diepctl);
 
-       DEBUG_IN_EP("%s:EP%d TX DMA start : DIEPDMA0 = 0x%x,"
-                   "DIEPTSIZ0 = 0x%x, DIEPCTL0 = 0x%x\n"
-                   "\tbuf = 0x%p, pktcnt = %d, xfersize = %d\n",
-                   __func__, ep_num,
-                   readl(&reg->in_endp[ep_num].diepdma),
-                   readl(&reg->in_endp[ep_num].dieptsiz),
-                   readl(&reg->in_endp[ep_num].diepctl),
-                   buf, pktcnt, length);
+       debug_cond(DEBUG_IN_EP,
+               "%s:EP%d TX DMA start : DIEPDMA0 = 0x%x,"
+               "DIEPTSIZ0 = 0x%x, DIEPCTL0 = 0x%x\n"
+               "\tbuf = 0x%p, pktcnt = %d, xfersize = %d\n",
+               __func__, ep_num,
+               readl(&reg->in_endp[ep_num].diepdma),
+               readl(&reg->in_endp[ep_num].dieptsiz),
+               readl(&reg->in_endp[ep_num].diepctl),
+               buf, pktcnt, length);
 
        return length;
 }
@@ -220,11 +191,11 @@ static void complete_rx(struct s3c_udc *dev, u8 ep_num)
        struct s3c_ep *ep = &dev->ep[ep_num];
        struct s3c_request *req = NULL;
        u32 ep_tsr = 0, xfer_size = 0, is_short = 0;
-       u32 *p = the_controller->dma_buf[ep_index(ep)+1];
 
        if (list_empty(&ep->queue)) {
-               DEBUG_OUT_EP("%s: RX DMA done : NULL REQ on OUT EP-%d\n",
-                                       __func__, ep_num);
+               debug_cond(DEBUG_OUT_EP != 0,
+                          "%s: RX DMA done : NULL REQ on OUT EP-%d\n",
+                          __func__, ep_num);
                return;
 
        }
@@ -239,22 +210,36 @@ static void complete_rx(struct s3c_udc *dev, u8 ep_num)
 
        xfer_size = ep->len - xfer_size;
 
-       invalidate_dcache_range((unsigned long) p,
-                               (unsigned long) p + DMA_BUFFER_SIZE);
-
-       memcpy(ep->dma_buf, p, ep->len);
+       /*
+        * NOTE:
+        *
+        * Please be careful with proper buffer allocation for USB request,
+        * which needs to be aligned to CONFIG_SYS_CACHELINE_SIZE, not only
+        * with starting address, but also its size shall be a cache line
+        * multiplication.
+        *
+        * This will prevent from corruption of data allocated immediatelly
+        * before or after the buffer.
+        *
+        * For armv7, the cache_v7.c provides proper code to emit "ERROR"
+        * message to warn users.
+        */
+       invalidate_dcache_range((unsigned long) ep->dma_buf,
+                               (unsigned long) ep->dma_buf +
+                               ROUND(xfer_size, CONFIG_SYS_CACHELINE_SIZE));
 
        req->req.actual += min(xfer_size, req->req.length - req->req.actual);
        is_short = (xfer_size < ep->ep.maxpacket);
 
-       DEBUG_OUT_EP("%s: RX DMA done : ep = %d, rx bytes = %d/%d, "
-                    "is_short = %d, DOEPTSIZ = 0x%x, remained bytes = %d\n",
-                       __func__, ep_num, req->req.actual, req->req.length,
-                       is_short, ep_tsr, xfer_size);
+       debug_cond(DEBUG_OUT_EP != 0,
+                  "%s: RX DMA done : ep = %d, rx bytes = %d/%d, "
+                  "is_short = %d, DOEPTSIZ = 0x%x, remained bytes = %d\n",
+                  __func__, ep_num, req->req.actual, req->req.length,
+                  is_short, ep_tsr, xfer_size);
 
        if (is_short || req->req.actual == req->req.length) {
                if (ep_num == EP0_CON && dev->ep0state == DATA_STATE_RECV) {
-                       DEBUG_OUT_EP("  => Send ZLP\n");
+                       debug_cond(DEBUG_OUT_EP != 0, " => Send ZLP\n");
                        s3c_udc_ep0_zlp(dev);
                        /* packet will be completed in complete_tx() */
                        dev->ep0state = WAIT_FOR_IN_COMPLETE;
@@ -264,8 +249,9 @@ static void complete_rx(struct s3c_udc *dev, u8 ep_num)
                        if (!list_empty(&ep->queue)) {
                                req = list_entry(ep->queue.next,
                                        struct s3c_request, queue);
-                               DEBUG_OUT_EP("%s: Next Rx request start...\n",
-                                        __func__);
+                               debug_cond(DEBUG_OUT_EP != 0,
+                                          "%s: Next Rx request start...\n",
+                                          __func__);
                                setdma_rx(ep, req);
                        }
                }
@@ -287,8 +273,9 @@ static void complete_tx(struct s3c_udc *dev, u8 ep_num)
        }
 
        if (list_empty(&ep->queue)) {
-               DEBUG_IN_EP("%s: TX DMA done : NULL REQ on IN EP-%d\n",
-                                       __func__, ep_num);
+               debug_cond(DEBUG_IN_EP,
+                       "%s: TX DMA done : NULL REQ on IN EP-%d\n",
+                       __func__, ep_num);
                return;
 
        }
@@ -301,33 +288,38 @@ static void complete_tx(struct s3c_udc *dev, u8 ep_num)
        is_short = (xfer_size < ep->ep.maxpacket);
        req->req.actual += min(xfer_size, req->req.length - req->req.actual);
 
-       DEBUG_IN_EP("%s: TX DMA done : ep = %d, tx bytes = %d/%d, "
-                    "is_short = %d, DIEPTSIZ = 0x%x, remained bytes = %d\n",
-                       __func__, ep_num, req->req.actual, req->req.length,
-                       is_short, ep_tsr, xfer_size);
+       debug_cond(DEBUG_IN_EP,
+               "%s: TX DMA done : ep = %d, tx bytes = %d/%d, "
+               "is_short = %d, DIEPTSIZ = 0x%x, remained bytes = %d\n",
+               __func__, ep_num, req->req.actual, req->req.length,
+               is_short, ep_tsr, xfer_size);
 
        if (ep_num == 0) {
                if (dev->ep0state == DATA_STATE_XMIT) {
-                       DEBUG_IN_EP("%s: ep_num = %d, ep0stat =="
-                                   "DATA_STATE_XMIT\n",
-                                   __func__, ep_num);
+                       debug_cond(DEBUG_IN_EP,
+                               "%s: ep_num = %d, ep0stat =="
+                               "DATA_STATE_XMIT\n",
+                               __func__, ep_num);
                        last = write_fifo_ep0(ep, req);
                        if (last)
                                dev->ep0state = WAIT_FOR_COMPLETE;
                } else if (dev->ep0state == WAIT_FOR_IN_COMPLETE) {
-                       DEBUG_IN_EP("%s: ep_num = %d, completing request\n",
-                                   __func__, ep_num);
+                       debug_cond(DEBUG_IN_EP,
+                               "%s: ep_num = %d, completing request\n",
+                               __func__, ep_num);
                        done(ep, req, 0);
                        dev->ep0state = WAIT_FOR_SETUP;
                } else if (dev->ep0state == WAIT_FOR_COMPLETE) {
-                       DEBUG_IN_EP("%s: ep_num = %d, completing request\n",
-                                   __func__, ep_num);
+                       debug_cond(DEBUG_IN_EP,
+                               "%s: ep_num = %d, completing request\n",
+                               __func__, ep_num);
                        done(ep, req, 0);
                        dev->ep0state = WAIT_FOR_OUT_COMPLETE;
                        s3c_ep0_complete_out();
                } else {
-                       DEBUG_IN_EP("%s: ep_num = %d, invalid ep state\n",
-                                   __func__, ep_num);
+                       debug_cond(DEBUG_IN_EP,
+                               "%s: ep_num = %d, invalid ep state\n",
+                               __func__, ep_num);
                }
                return;
        }
@@ -337,7 +329,8 @@ static void complete_tx(struct s3c_udc *dev, u8 ep_num)
 
        if (!list_empty(&ep->queue)) {
                req = list_entry(ep->queue.next, struct s3c_request, queue);
-               DEBUG_IN_EP("%s: Next Tx request start...\n", __func__);
+               debug_cond(DEBUG_IN_EP,
+                       "%s: Next Tx request start...\n", __func__);
                setdma_tx(ep, req);
        }
 }
@@ -347,19 +340,22 @@ static inline void s3c_udc_check_tx_queue(struct s3c_udc *dev, u8 ep_num)
        struct s3c_ep *ep = &dev->ep[ep_num];
        struct s3c_request *req;
 
-       DEBUG_IN_EP("%s: Check queue, ep_num = %d\n", __func__, ep_num);
+       debug_cond(DEBUG_IN_EP,
+               "%s: Check queue, ep_num = %d\n", __func__, ep_num);
 
        if (!list_empty(&ep->queue)) {
                req = list_entry(ep->queue.next, struct s3c_request, queue);
-               DEBUG_IN_EP("%s: Next Tx request(0x%p) start...\n",
-                           __func__, req);
+               debug_cond(DEBUG_IN_EP,
+                       "%s: Next Tx request(0x%p) start...\n",
+                       __func__, req);
 
                if (ep_is_in(ep))
                        setdma_tx(ep, req);
                else
                        setdma_rx(ep, req);
        } else {
-               DEBUG_IN_EP("%s: NULL REQ on IN EP-%d\n", __func__, ep_num);
+               debug_cond(DEBUG_IN_EP,
+                       "%s: NULL REQ on IN EP-%d\n", __func__, ep_num);
 
                return;
        }
@@ -372,16 +368,17 @@ static void process_ep_in_intr(struct s3c_udc *dev)
        u8 ep_num = 0;
 
        ep_intr = readl(&reg->daint);
-       DEBUG_IN_EP("*** %s: EP In interrupt : DAINT = 0x%x\n",
-                               __func__, ep_intr);
+       debug_cond(DEBUG_IN_EP,
+               "*** %s: EP In interrupt : DAINT = 0x%x\n", __func__, ep_intr);
 
        ep_intr &= DAINT_MASK;
 
        while (ep_intr) {
                if (ep_intr & DAINT_IN_EP_INT(1)) {
                        ep_intr_status = readl(&reg->in_endp[ep_num].diepint);
-                       DEBUG_IN_EP("\tEP%d-IN : DIEPINT = 0x%x\n",
-                                               ep_num, ep_intr_status);
+                       debug_cond(DEBUG_IN_EP,
+                                  "\tEP%d-IN : DIEPINT = 0x%x\n",
+                                  ep_num, ep_intr_status);
 
                        /* Interrupt Clear */
                        writel(ep_intr_status, &reg->in_endp[ep_num].diepint);
@@ -418,16 +415,18 @@ static void process_ep_out_intr(struct s3c_udc *dev)
        u8 ep_num = 0;
 
        ep_intr = readl(&reg->daint);
-       DEBUG_OUT_EP("*** %s: EP OUT interrupt : DAINT = 0x%x\n",
-                               __func__, ep_intr);
+       debug_cond(DEBUG_OUT_EP != 0,
+                  "*** %s: EP OUT interrupt : DAINT = 0x%x\n",
+                  __func__, ep_intr);
 
        ep_intr = (ep_intr >> DAINT_OUT_BIT) & DAINT_MASK;
 
        while (ep_intr) {
                if (ep_intr & 0x1) {
                        ep_intr_status = readl(&reg->out_endp[ep_num].doepint);
-                       DEBUG_OUT_EP("\tEP%d-OUT : DOEPINT = 0x%x\n",
-                                               ep_num, ep_intr_status);
+                       debug_cond(DEBUG_OUT_EP != 0,
+                                  "\tEP%d-OUT : DOEPINT = 0x%x\n",
+                                  ep_num, ep_intr_status);
 
                        /* Interrupt Clear */
                        writel(ep_intr_status, &reg->out_endp[ep_num].doepint);
@@ -445,7 +444,8 @@ static void process_ep_out_intr(struct s3c_udc *dev)
 
                                if (ep_intr_status &
                                    CTRL_OUT_EP_SETUP_PHASE_DONE) {
-                                       DEBUG_OUT_EP("SETUP packet arrived\n");
+                                       debug_cond(DEBUG_OUT_EP != 0,
+                                                  "SETUP packet arrived\n");
                                        s3c_handle_ep0(dev);
                                }
                        } else {
@@ -473,7 +473,8 @@ static int s3c_udc_irq(int irq, void *_dev)
        intr_status = readl(&reg->gintsts);
        gintmsk = readl(&reg->gintmsk);
 
-       DEBUG_ISR("\n*** %s : GINTSTS=0x%x(on state %s), GINTMSK : 0x%x,"
+       debug_cond(DEBUG_ISR,
+                 "\n*** %s : GINTSTS=0x%x(on state %s), GINTMSK : 0x%x,"
                  "DAINT : 0x%x, DAINTMSK : 0x%x\n",
                  __func__, intr_status, state_names[dev->ep0state], gintmsk,
                  readl(&reg->daint), readl(&reg->daintmsk));
@@ -484,30 +485,33 @@ static int s3c_udc_irq(int irq, void *_dev)
        }
 
        if (intr_status & INT_ENUMDONE) {
-               DEBUG_ISR("\tSpeed Detection interrupt\n");
+               debug_cond(DEBUG_ISR, "\tSpeed Detection interrupt\n");
 
                writel(INT_ENUMDONE, &reg->gintsts);
                usb_status = (readl(&reg->dsts) & 0x6);
 
                if (usb_status & (USB_FULL_30_60MHZ | USB_FULL_48MHZ)) {
-                       DEBUG_ISR("\t\tFull Speed Detection\n");
+                       debug_cond(DEBUG_ISR,
+                                  "\t\tFull Speed Detection\n");
                        set_max_pktsize(dev, USB_SPEED_FULL);
 
                } else {
-                       DEBUG_ISR("\t\tHigh Speed Detection : 0x%x\n",
-                                 usb_status);
+                       debug_cond(DEBUG_ISR,
+                               "\t\tHigh Speed Detection : 0x%x\n",
+                               usb_status);
                        set_max_pktsize(dev, USB_SPEED_HIGH);
                }
        }
 
        if (intr_status & INT_EARLY_SUSPEND) {
-               DEBUG_ISR("\tEarly suspend interrupt\n");
+               debug_cond(DEBUG_ISR, "\tEarly suspend interrupt\n");
                writel(INT_EARLY_SUSPEND, &reg->gintsts);
        }
 
        if (intr_status & INT_SUSPEND) {
                usb_status = readl(&reg->dsts);
-               DEBUG_ISR("\tSuspend interrupt :(DSTS):0x%x\n", usb_status);
+               debug_cond(DEBUG_ISR,
+                       "\tSuspend interrupt :(DSTS):0x%x\n", usb_status);
                writel(INT_SUSPEND, &reg->gintsts);
 
                if (dev->gadget.speed != USB_SPEED_UNKNOWN
@@ -525,7 +529,7 @@ static int s3c_udc_irq(int irq, void *_dev)
        }
 
        if (intr_status & INT_RESUME) {
-               DEBUG_ISR("\tResume interrupt\n");
+               debug_cond(DEBUG_ISR, "\tResume interrupt\n");
                writel(INT_RESUME, &reg->gintsts);
 
                if (dev->gadget.speed != USB_SPEED_UNKNOWN
@@ -538,13 +542,15 @@ static int s3c_udc_irq(int irq, void *_dev)
 
        if (intr_status & INT_RESET) {
                usb_status = readl(&reg->gotgctl);
-               DEBUG_ISR("\tReset interrupt - (GOTGCTL):0x%x\n", usb_status);
+               debug_cond(DEBUG_ISR,
+                       "\tReset interrupt - (GOTGCTL):0x%x\n", usb_status);
                writel(INT_RESET, &reg->gintsts);
 
                if ((usb_status & 0xc0000) == (0x3 << 18)) {
                        if (reset_available) {
-                               DEBUG_ISR("\t\tOTG core got reset (%d)!!\n",
-                                         reset_available);
+                               debug_cond(DEBUG_ISR,
+                                       "\t\tOTG core got reset (%d)!!\n",
+                                       reset_available);
                                reconfig_usbd();
                                dev->ep0state = WAIT_FOR_SETUP;
                                reset_available = 0;
@@ -554,7 +560,8 @@ static int s3c_udc_irq(int irq, void *_dev)
 
                } else {
                        reset_available = 1;
-                       DEBUG_ISR("\t\tRESET handling skipped\n");
+                       debug_cond(DEBUG_ISR,
+                                  "\t\tRESET handling skipped\n");
                }
        }
 
@@ -585,7 +592,7 @@ static int s3c_queue(struct usb_ep *_ep, struct usb_request *_req,
        if (unlikely(!_req || !_req->complete || !_req->buf
                     || !list_empty(&req->queue))) {
 
-               DEBUG("%s: bad params\n", __func__);
+               debug("%s: bad params\n", __func__);
                return -EINVAL;
        }
 
@@ -593,7 +600,7 @@ static int s3c_queue(struct usb_ep *_ep, struct usb_request *_req,
 
        if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
 
-               DEBUG("%s: bad ep: %s, %d, %x\n", __func__,
+               debug("%s: bad ep: %s, %d, %p\n", __func__,
                      ep->ep.name, !ep->desc, _ep);
                return -EINVAL;
        }
@@ -602,7 +609,7 @@ static int s3c_queue(struct usb_ep *_ep, struct usb_request *_req,
        dev = ep->dev;
        if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
 
-               DEBUG("%s: bogus device state %p\n", __func__, dev->driver);
+               debug("%s: bogus device state %p\n", __func__, dev->driver);
                return -ESHUTDOWN;
        }
 
@@ -612,13 +619,13 @@ static int s3c_queue(struct usb_ep *_ep, struct usb_request *_req,
        _req->actual = 0;
 
        /* kickstart this i/o queue? */
-       DEBUG("\n*** %s: %s-%s req = %p, len = %d, buf = %p"
+       debug("\n*** %s: %s-%s req = %p, len = %d, buf = %p"
                "Q empty = %d, stopped = %d\n",
                __func__, _ep->name, ep_is_in(ep) ? "in" : "out",
                _req, _req->length, _req->buf,
                list_empty(&ep->queue), ep->stopped);
 
-#ifdef DEBUG_S3C_UDC
+#ifdef DEBUG
        {
                int i, len = _req->length;
 
@@ -644,14 +651,16 @@ static int s3c_queue(struct usb_ep *_ep, struct usb_request *_req,
 
                } else if (ep_is_in(ep)) {
                        gintsts = readl(&reg->gintsts);
-                       DEBUG_IN_EP("%s: ep_is_in, S3C_UDC_OTG_GINTSTS=0x%x\n",
-                                               __func__, gintsts);
+                       debug_cond(DEBUG_IN_EP,
+                                  "%s: ep_is_in, S3C_UDC_OTG_GINTSTS=0x%x\n",
+                                  __func__, gintsts);
 
                        setdma_tx(ep, req);
                } else {
                        gintsts = readl(&reg->gintsts);
-                       DEBUG_OUT_EP("%s:ep_is_out, S3C_UDC_OTG_GINTSTS=0x%x\n",
-                               __func__, gintsts);
+                       debug_cond(DEBUG_OUT_EP != 0,
+                                  "%s:ep_is_out, S3C_UDC_OTG_GINTSTS=0x%x\n",
+                                  __func__, gintsts);
 
                        setdma_rx(ep, req);
                }
@@ -679,7 +688,7 @@ static int write_fifo_ep0(struct s3c_ep *ep, struct s3c_request *req)
 
        max = ep_maxpacket(ep);
 
-       DEBUG_EP0("%s: max = %d\n", __func__, max);
+       debug_cond(DEBUG_EP0 != 0, "%s: max = %d\n", __func__, max);
 
        count = setdma_tx(ep, req);
 
@@ -694,10 +703,11 @@ static int write_fifo_ep0(struct s3c_ep *ep, struct s3c_request *req)
                        is_last = 1;
        }
 
-       DEBUG_EP0("%s: wrote %s %d bytes%s %d left %p\n", __func__,
-                 ep->ep.name, count,
-                 is_last ? "/L" : "",
-                 req->req.length - req->req.actual - count, req);
+       debug_cond(DEBUG_EP0 != 0,
+                  "%s: wrote %s %d bytes%s %d left %p\n", __func__,
+                  ep->ep.name, count,
+                  is_last ? "/L" : "",
+                  req->req.length - req->req.actual - count, req);
 
        /* requests complete when all IN data is in the FIFO */
        if (is_last) {
@@ -710,18 +720,14 @@ static int write_fifo_ep0(struct s3c_ep *ep, struct s3c_request *req)
 
 int s3c_fifo_read(struct s3c_ep *ep, u32 *cp, int max)
 {
-       u32 bytes;
-
-       bytes = sizeof(struct usb_ctrlrequest);
+       invalidate_dcache_range((unsigned long)cp, (unsigned long)cp +
+                               ROUND(max, CONFIG_SYS_CACHELINE_SIZE));
 
-       invalidate_dcache_range((unsigned long) ep->dev->dma_buf[ep_index(ep)],
-                               (unsigned long) ep->dev->dma_buf[ep_index(ep)]
-                               + DMA_BUFFER_SIZE);
+       debug_cond(DEBUG_EP0 != 0,
+                  "%s: bytes=%d, ep_index=%d 0x%p\n", __func__,
+                  max, ep_index(ep), cp);
 
-       DEBUG_EP0("%s: bytes=%d, ep_index=%d %p\n", __func__,
-                 bytes, ep_index(ep), ep->dev->dma_buf[ep_index(ep)]);
-
-       return bytes;
+       return max;
 }
 
 /**
@@ -738,8 +744,9 @@ static void udc_set_address(struct s3c_udc *dev, unsigned char address)
 
        s3c_udc_ep0_zlp(dev);
 
-       DEBUG_EP0("%s: USB OTG 2.0 Device address=%d, DCFG=0x%x\n",
-               __func__, address, readl(&reg->dcfg));
+       debug_cond(DEBUG_EP0 != 0,
+                  "%s: USB OTG 2.0 Device address=%d, DCFG=0x%x\n",
+                  __func__, address, readl(&reg->dcfg));
 
        dev->usb_address = address;
 }
@@ -760,8 +767,9 @@ static inline void s3c_udc_ep0_set_stall(struct s3c_ep *ep)
 
        writel(ep_ctrl, &reg->in_endp[EP0_CON].diepctl);
 
-       DEBUG_EP0("%s: set ep%d stall, DIEPCTL0 = 0x%x\n",
-               __func__, ep_index(ep), &reg->in_endp[EP0_CON].diepctl);
+       debug_cond(DEBUG_EP0 != 0,
+                  "%s: set ep%d stall, DIEPCTL0 = 0x%p\n",
+                  __func__, ep_index(ep), &reg->in_endp[EP0_CON].diepctl);
        /*
         * The application can only set this bit, and the core clears it,
         * when a SETUP token is received for this endpoint
@@ -775,19 +783,19 @@ static void s3c_ep0_read(struct s3c_udc *dev)
 {
        struct s3c_request *req;
        struct s3c_ep *ep = &dev->ep[0];
-       int ret;
 
        if (!list_empty(&ep->queue)) {
                req = list_entry(ep->queue.next, struct s3c_request, queue);
 
        } else {
-               DEBUG("%s: ---> BUG\n", __func__);
+               debug("%s: ---> BUG\n", __func__);
                BUG();
                return;
        }
 
-       DEBUG_EP0("%s: req = %p, req.length = 0x%x, req.actual = 0x%x\n",
-               __func__, req, req->req.length, req->req.actual);
+       debug_cond(DEBUG_EP0 != 0,
+                  "%s: req = %p, req.length = 0x%x, req.actual = 0x%x\n",
+                  __func__, req, req->req.length, req->req.actual);
 
        if (req->req.length == 0) {
                /* zlp for Set_configuration, Set_interface,
@@ -796,12 +804,13 @@ static void s3c_ep0_read(struct s3c_udc *dev)
                ep->len = 0;
                s3c_udc_ep0_zlp(dev);
 
-               DEBUG_EP0("%s: req.length = 0, bRequest = %d\n",
-                         __func__, usb_ctrl->bRequest);
+               debug_cond(DEBUG_EP0 != 0,
+                          "%s: req.length = 0, bRequest = %d\n",
+                          __func__, usb_ctrl->bRequest);
                return;
        }
 
-       ret = setdma_rx(ep, req);
+       setdma_rx(ep, req);
 }
 
 /*
@@ -819,12 +828,13 @@ static int s3c_ep0_write(struct s3c_udc *dev)
                req = list_entry(ep->queue.next, struct s3c_request, queue);
 
        if (!req) {
-               DEBUG_EP0("%s: NULL REQ\n", __func__);
+               debug_cond(DEBUG_EP0 != 0, "%s: NULL REQ\n", __func__);
                return 0;
        }
 
-       DEBUG_EP0("%s: req = %p, req.length = 0x%x, req.actual = 0x%x\n",
-               __func__, req, req->req.length, req->req.actual);
+       debug_cond(DEBUG_EP0 != 0,
+                  "%s: req = %p, req.length = 0x%x, req.actual = 0x%x\n",
+                  __func__, req, req->req.length, req->req.actual);
 
        if (req->req.length - req->req.actual == ep0_fifo_size) {
                /* Next write will end with the packet size, */
@@ -837,49 +847,54 @@ static int s3c_ep0_write(struct s3c_udc *dev)
        if ((ret == 1) && !need_zlp) {
                /* Last packet */
                dev->ep0state = WAIT_FOR_COMPLETE;
-               DEBUG_EP0("%s: finished, waiting for status\n", __func__);
+               debug_cond(DEBUG_EP0 != 0,
+                          "%s: finished, waiting for status\n", __func__);
 
        } else {
                dev->ep0state = DATA_STATE_XMIT;
-               DEBUG_EP0("%s: not finished\n", __func__);
+               debug_cond(DEBUG_EP0 != 0,
+                          "%s: not finished\n", __func__);
        }
 
        return 1;
 }
 
-u16    g_status;
-
 int s3c_udc_get_status(struct s3c_udc *dev,
                struct usb_ctrlrequest *crq)
 {
        u8 ep_num = crq->wIndex & 0x7F;
+       u16 g_status = 0;
        u32 ep_ctrl;
-       u32 *p = the_controller->dma_buf[1];
 
-       DEBUG_SETUP("%s: *** USB_REQ_GET_STATUS\n", __func__);
+       debug_cond(DEBUG_SETUP != 0,
+                  "%s: *** USB_REQ_GET_STATUS\n", __func__);
        printf("crq->brequest:0x%x\n", crq->bRequestType & USB_RECIP_MASK);
        switch (crq->bRequestType & USB_RECIP_MASK) {
        case USB_RECIP_INTERFACE:
                g_status = 0;
-               DEBUG_SETUP("\tGET_STATUS:USB_RECIP_INTERFACE, g_stauts = %d\n",
-                           g_status);
+               debug_cond(DEBUG_SETUP != 0,
+                          "\tGET_STATUS:USB_RECIP_INTERFACE, g_stauts = %d\n",
+                          g_status);
                break;
 
        case USB_RECIP_DEVICE:
                g_status = 0x1; /* Self powered */
-               DEBUG_SETUP("\tGET_STATUS: USB_RECIP_DEVICE, g_stauts = %d\n",
-                           g_status);
+               debug_cond(DEBUG_SETUP != 0,
+                          "\tGET_STATUS: USB_RECIP_DEVICE, g_stauts = %d\n",
+                          g_status);
                break;
 
        case USB_RECIP_ENDPOINT:
                if (crq->wLength > 2) {
-                       DEBUG_SETUP("\tGET_STATUS:Not support EP or wLength\n");
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "\tGET_STATUS:Not support EP or wLength\n");
                        return 1;
                }
 
                g_status = dev->ep[ep_num].stopped;
-               DEBUG_SETUP("\tGET_STATUS: USB_RECIP_ENDPOINT, g_stauts = %d\n",
-                           g_status);
+               debug_cond(DEBUG_SETUP != 0,
+                          "\tGET_STATUS: USB_RECIP_ENDPOINT, g_stauts = %d\n",
+                          g_status);
 
                break;
 
@@ -887,12 +902,13 @@ int s3c_udc_get_status(struct s3c_udc *dev,
                return 1;
        }
 
-       memcpy(p, &g_status, sizeof(g_status));
+       memcpy(usb_ctrl, &g_status, sizeof(g_status));
 
-       flush_dcache_range((unsigned long) p,
-                          (unsigned long) p + DMA_BUFFER_SIZE);
+       flush_dcache_range((unsigned long) usb_ctrl,
+                          (unsigned long) usb_ctrl +
+                          ROUND(sizeof(g_status), CONFIG_SYS_CACHELINE_SIZE));
 
-       writel(the_controller->dma_addr[1], &reg->in_endp[EP0_CON].diepdma);
+       writel(usb_ctrl_dma_addr, &reg->in_endp[EP0_CON].diepdma);
        writel(DIEPT_SIZ_PKT_CNT(1) | DIEPT_SIZ_XFER_SIZE(2),
               &reg->in_endp[EP0_CON].dieptsiz);
 
@@ -910,19 +926,19 @@ static void s3c_udc_set_nak(struct s3c_ep *ep)
        u32             ep_ctrl = 0;
 
        ep_num = ep_index(ep);
-       DEBUG("%s: ep_num = %d, ep_type = %d\n", __func__, ep_num, ep->ep_type);
+       debug("%s: ep_num = %d, ep_type = %d\n", __func__, ep_num, ep->ep_type);
 
        if (ep_is_in(ep)) {
                ep_ctrl = readl(&reg->in_endp[ep_num].diepctl);
                ep_ctrl |= DEPCTL_SNAK;
                writel(ep_ctrl, &reg->in_endp[ep_num].diepctl);
-               DEBUG("%s: set NAK, DIEPCTL%d = 0x%x\n",
+               debug("%s: set NAK, DIEPCTL%d = 0x%x\n",
                        __func__, ep_num, readl(&reg->in_endp[ep_num].diepctl));
        } else {
                ep_ctrl = readl(&reg->out_endp[ep_num].doepctl);
                ep_ctrl |= DEPCTL_SNAK;
                writel(ep_ctrl, &reg->out_endp[ep_num].doepctl);
-               DEBUG("%s: set NAK, DOEPCTL%d = 0x%x\n",
+               debug("%s: set NAK, DOEPCTL%d = 0x%x\n",
                      __func__, ep_num, readl(&reg->out_endp[ep_num].doepctl));
        }
 
@@ -936,7 +952,7 @@ void s3c_udc_ep_set_stall(struct s3c_ep *ep)
        u32             ep_ctrl = 0;
 
        ep_num = ep_index(ep);
-       DEBUG("%s: ep_num = %d, ep_type = %d\n", __func__, ep_num, ep->ep_type);
+       debug("%s: ep_num = %d, ep_type = %d\n", __func__, ep_num, ep->ep_type);
 
        if (ep_is_in(ep)) {
                ep_ctrl = readl(&reg->in_endp[ep_num].diepctl);
@@ -948,7 +964,7 @@ void s3c_udc_ep_set_stall(struct s3c_ep *ep)
                ep_ctrl |= DEPCTL_STALL;
 
                writel(ep_ctrl, &reg->in_endp[ep_num].diepctl);
-               DEBUG("%s: set stall, DIEPCTL%d = 0x%x\n",
+               debug("%s: set stall, DIEPCTL%d = 0x%x\n",
                      __func__, ep_num, readl(&reg->in_endp[ep_num].diepctl));
 
        } else {
@@ -958,7 +974,7 @@ void s3c_udc_ep_set_stall(struct s3c_ep *ep)
                ep_ctrl |= DEPCTL_STALL;
 
                writel(ep_ctrl, &reg->out_endp[ep_num].doepctl);
-               DEBUG("%s: set stall, DOEPCTL%d = 0x%x\n",
+               debug("%s: set stall, DOEPCTL%d = 0x%x\n",
                      __func__, ep_num, readl(&reg->out_endp[ep_num].doepctl));
        }
 
@@ -971,7 +987,7 @@ void s3c_udc_ep_clear_stall(struct s3c_ep *ep)
        u32             ep_ctrl = 0;
 
        ep_num = ep_index(ep);
-       DEBUG("%s: ep_num = %d, ep_type = %d\n", __func__, ep_num, ep->ep_type);
+       debug("%s: ep_num = %d, ep_type = %d\n", __func__, ep_num, ep->ep_type);
 
        if (ep_is_in(ep)) {
                ep_ctrl = readl(&reg->in_endp[ep_num].diepctl);
@@ -991,7 +1007,7 @@ void s3c_udc_ep_clear_stall(struct s3c_ep *ep)
                }
 
                writel(ep_ctrl, &reg->in_endp[ep_num].diepctl);
-               DEBUG("%s: cleared stall, DIEPCTL%d = 0x%x\n",
+               debug("%s: cleared stall, DIEPCTL%d = 0x%x\n",
                        __func__, ep_num, readl(&reg->in_endp[ep_num].diepctl));
 
        } else {
@@ -1006,7 +1022,7 @@ void s3c_udc_ep_clear_stall(struct s3c_ep *ep)
                }
 
                writel(ep_ctrl, &reg->out_endp[ep_num].doepctl);
-               DEBUG("%s: cleared stall, DOEPCTL%d = 0x%x\n",
+               debug("%s: cleared stall, DOEPCTL%d = 0x%x\n",
                      __func__, ep_num, readl(&reg->out_endp[ep_num].doepctl));
        }
 
@@ -1025,14 +1041,14 @@ static int s3c_udc_set_halt(struct usb_ep *_ep, int value)
 
        if (unlikely(!_ep || !ep->desc || ep_num == EP0_CON ||
                     ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC)) {
-               DEBUG("%s: %s bad ep or descriptor\n", __func__, ep->ep.name);
+               debug("%s: %s bad ep or descriptor\n", __func__, ep->ep.name);
                return -EINVAL;
        }
 
        /* Attempt to halt IN ep will fail if any transfer requests
         * are still queue */
        if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
-               DEBUG("%s: %s queue not empty, req = %p\n",
+               debug("%s: %s queue not empty, req = %p\n",
                        __func__, ep->ep.name,
                        list_entry(ep->queue.next, struct s3c_request, queue));
 
@@ -1040,7 +1056,7 @@ static int s3c_udc_set_halt(struct usb_ep *_ep, int value)
        }
 
        dev = ep->dev;
-       DEBUG("%s: ep_num = %d, value = %d\n", __func__, ep_num, value);
+       debug("%s: ep_num = %d, value = %d\n", __func__, ep_num, value);
 
        spin_lock_irqsave(&dev->lock, flags);
 
@@ -1076,7 +1092,7 @@ void s3c_udc_ep_activate(struct s3c_ep *ep)
                daintmsk = (1 << ep_num) << DAINT_OUT_BIT;
        }
 
-       DEBUG("%s: EPCTRL%d = 0x%x, ep_is_in = %d\n",
+       debug("%s: EPCTRL%d = 0x%x, ep_is_in = %d\n",
                __func__, ep_num, ep_ctrl, ep_is_in(ep));
 
        /* If the EP is already active don't change the EP Control
@@ -1090,12 +1106,12 @@ void s3c_udc_ep_activate(struct s3c_ep *ep)
 
                if (ep_is_in(ep)) {
                        writel(ep_ctrl, &reg->in_endp[ep_num].diepctl);
-                       DEBUG("%s: USB Ative EP%d, DIEPCTRL%d = 0x%x\n",
+                       debug("%s: USB Ative EP%d, DIEPCTRL%d = 0x%x\n",
                              __func__, ep_num, ep_num,
                              readl(&reg->in_endp[ep_num].diepctl));
                } else {
                        writel(ep_ctrl, &reg->out_endp[ep_num].doepctl);
-                       DEBUG("%s: USB Ative EP%d, DOEPCTRL%d = 0x%x\n",
+                       debug("%s: USB Ative EP%d, DOEPCTRL%d = 0x%x\n",
                              __func__, ep_num, ep_num,
                              readl(&reg->out_endp[ep_num].doepctl));
                }
@@ -1103,7 +1119,7 @@ void s3c_udc_ep_activate(struct s3c_ep *ep)
 
        /* Unmask EP Interrtupt */
        writel(readl(&reg->daintmsk)|daintmsk, &reg->daintmsk);
-       DEBUG("%s: DAINTMSK = 0x%x\n", __func__, readl(&reg->daintmsk));
+       debug("%s: DAINTMSK = 0x%x\n", __func__, readl(&reg->daintmsk));
 
 }
 
@@ -1117,11 +1133,13 @@ static int s3c_udc_clear_feature(struct usb_ep *_ep)
        ep_num = ep_index(ep);
 
        dev = ep->dev;
-       DEBUG_SETUP("%s: ep_num = %d, is_in = %d, clear_feature_flag = %d\n",
-               __func__, ep_num, ep_is_in(ep), clear_feature_flag);
+       debug_cond(DEBUG_SETUP != 0,
+                  "%s: ep_num = %d, is_in = %d, clear_feature_flag = %d\n",
+                  __func__, ep_num, ep_is_in(ep), clear_feature_flag);
 
        if (usb_ctrl->wLength != 0) {
-               DEBUG_SETUP("\tCLEAR_FEATURE: wLength is not zero.....\n");
+               debug_cond(DEBUG_SETUP != 0,
+                          "\tCLEAR_FEATURE: wLength is not zero.....\n");
                return 1;
        }
 
@@ -1129,11 +1147,13 @@ static int s3c_udc_clear_feature(struct usb_ep *_ep)
        case USB_RECIP_DEVICE:
                switch (usb_ctrl->wValue) {
                case USB_DEVICE_REMOTE_WAKEUP:
-                       DEBUG_SETUP("\tOFF:USB_DEVICE_REMOTE_WAKEUP\n");
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "\tOFF:USB_DEVICE_REMOTE_WAKEUP\n");
                        break;
 
                case USB_DEVICE_TEST_MODE:
-                       DEBUG_SETUP("\tCLEAR_FEATURE: USB_DEVICE_TEST_MODE\n");
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "\tCLEAR_FEATURE: USB_DEVICE_TEST_MODE\n");
                        /** @todo Add CLEAR_FEATURE for TEST modes. */
                        break;
                }
@@ -1142,8 +1162,9 @@ static int s3c_udc_clear_feature(struct usb_ep *_ep)
                break;
 
        case USB_RECIP_ENDPOINT:
-               DEBUG_SETUP("\tCLEAR_FEATURE:USB_RECIP_ENDPOINT, wValue = %d\n",
-                               usb_ctrl->wValue);
+               debug_cond(DEBUG_SETUP != 0,
+                          "\tCLEAR_FEATURE:USB_RECIP_ENDPOINT, wValue = %d\n",
+                          usb_ctrl->wValue);
 
                if (usb_ctrl->wValue == USB_ENDPOINT_HALT) {
                        if (ep_num == 0) {
@@ -1176,11 +1197,13 @@ static int s3c_udc_set_feature(struct usb_ep *_ep)
        ep_num = ep_index(ep);
        dev = ep->dev;
 
-       DEBUG_SETUP("%s: *** USB_REQ_SET_FEATURE , ep_num = %d\n",
+       debug_cond(DEBUG_SETUP != 0,
+                  "%s: *** USB_REQ_SET_FEATURE , ep_num = %d\n",
                    __func__, ep_num);
 
        if (usb_ctrl->wLength != 0) {
-               DEBUG_SETUP("\tSET_FEATURE: wLength is not zero.....\n");
+               debug_cond(DEBUG_SETUP != 0,
+                          "\tSET_FEATURE: wLength is not zero.....\n");
                return 1;
        }
 
@@ -1188,20 +1211,24 @@ static int s3c_udc_set_feature(struct usb_ep *_ep)
        case USB_RECIP_DEVICE:
                switch (usb_ctrl->wValue) {
                case USB_DEVICE_REMOTE_WAKEUP:
-                       DEBUG_SETUP("\tSET_FEATURE:USB_DEVICE_REMOTE_WAKEUP\n");
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "\tSET_FEATURE:USB_DEVICE_REMOTE_WAKEUP\n");
                        break;
                case USB_DEVICE_B_HNP_ENABLE:
-                       DEBUG_SETUP("\tSET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "\tSET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
                        break;
 
                case USB_DEVICE_A_HNP_SUPPORT:
                        /* RH port supports HNP */
-                       DEBUG_SETUP("\tSET_FEATURE:USB_DEVICE_A_HNP_SUPPORT\n");
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "\tSET_FEATURE:USB_DEVICE_A_HNP_SUPPORT\n");
                        break;
 
                case USB_DEVICE_A_ALT_HNP_SUPPORT:
                        /* other RH port does */
-                       DEBUG_SETUP("\tSET: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "\tSET: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
                        break;
                }
 
@@ -1209,11 +1236,13 @@ static int s3c_udc_set_feature(struct usb_ep *_ep)
                return 0;
 
        case USB_RECIP_INTERFACE:
-               DEBUG_SETUP("\tSET_FEATURE: USB_RECIP_INTERFACE\n");
+               debug_cond(DEBUG_SETUP != 0,
+                          "\tSET_FEATURE: USB_RECIP_INTERFACE\n");
                break;
 
        case USB_RECIP_ENDPOINT:
-               DEBUG_SETUP("\tSET_FEATURE: USB_RECIP_ENDPOINT\n");
+               debug_cond(DEBUG_SETUP != 0,
+                          "\tSET_FEATURE: USB_RECIP_ENDPOINT\n");
                if (usb_ctrl->wValue == USB_ENDPOINT_HALT) {
                        if (ep_num == 0) {
                                s3c_udc_ep0_set_stall(ep);
@@ -1236,26 +1265,27 @@ static int s3c_udc_set_feature(struct usb_ep *_ep)
 void s3c_ep0_setup(struct s3c_udc *dev)
 {
        struct s3c_ep *ep = &dev->ep[0];
-       int i, bytes, is_in;
+       int i;
        u8 ep_num;
 
        /* Nuke all previous transfers */
        nuke(ep, -EPROTO);
 
        /* read control req from fifo (8 bytes) */
-       bytes = s3c_fifo_read(ep, (u32 *)usb_ctrl, 8);
+       s3c_fifo_read(ep, (u32 *)usb_ctrl, 8);
 
-       DEBUG_SETUP("%s: bRequestType = 0x%x(%s), bRequest = 0x%x"
-                   "\twLength = 0x%x, wValue = 0x%x, wIndex= 0x%x\n",
-                   __func__, usb_ctrl->bRequestType,
-                   (usb_ctrl->bRequestType & USB_DIR_IN) ? "IN" : "OUT",
-                   usb_ctrl->bRequest,
-                   usb_ctrl->wLength, usb_ctrl->wValue, usb_ctrl->wIndex);
+       debug_cond(DEBUG_SETUP != 0,
+                  "%s: bRequestType = 0x%x(%s), bRequest = 0x%x"
+                  "\twLength = 0x%x, wValue = 0x%x, wIndex= 0x%x\n",
+                  __func__, usb_ctrl->bRequestType,
+                  (usb_ctrl->bRequestType & USB_DIR_IN) ? "IN" : "OUT",
+                  usb_ctrl->bRequest,
+                  usb_ctrl->wLength, usb_ctrl->wValue, usb_ctrl->wIndex);
 
-#ifdef DEBUG_S3C_UDC
+#ifdef DEBUG
        {
                int i, len = sizeof(*usb_ctrl);
-               char *p = usb_ctrl;
+               char *p = (char *)usb_ctrl;
 
                printf("pkt = ");
                for (i = 0; i < len; i++) {
@@ -1269,10 +1299,12 @@ void s3c_ep0_setup(struct s3c_udc *dev)
 
        if (usb_ctrl->bRequest == GET_MAX_LUN_REQUEST &&
            usb_ctrl->wLength != 1) {
-               DEBUG_SETUP("\t%s:GET_MAX_LUN_REQUEST:invalid",
-                             __func__);
-               DEBUG_SETUP("wLength = %d, setup returned\n",
-                           usb_ctrl->wLength);
+               debug_cond(DEBUG_SETUP != 0,
+                          "\t%s:GET_MAX_LUN_REQUEST:invalid",
+                          __func__);
+               debug_cond(DEBUG_SETUP != 0,
+                          "wLength = %d, setup returned\n",
+                          usb_ctrl->wLength);
 
                s3c_udc_ep0_set_stall(ep);
                dev->ep0state = WAIT_FOR_SETUP;
@@ -1281,8 +1313,9 @@ void s3c_ep0_setup(struct s3c_udc *dev)
        } else if (usb_ctrl->bRequest == BOT_RESET_REQUEST &&
                 usb_ctrl->wLength != 0) {
                /* Bulk-Only *mass storge reset of class-specific request */
-               DEBUG_SETUP("%s:BOT Rest:invalid wLength =%d, setup returned\n",
-                           __func__, usb_ctrl->wLength);
+               debug_cond(DEBUG_SETUP != 0,
+                          "%s:BOT Rest:invalid wLength =%d, setup returned\n",
+                          __func__, usb_ctrl->wLength);
 
                s3c_udc_ep0_set_stall(ep);
                dev->ep0state = WAIT_FOR_SETUP;
@@ -1293,24 +1326,22 @@ void s3c_ep0_setup(struct s3c_udc *dev)
        /* Set direction of EP0 */
        if (likely(usb_ctrl->bRequestType & USB_DIR_IN)) {
                ep->bEndpointAddress |= USB_DIR_IN;
-               is_in = 1;
-
        } else {
                ep->bEndpointAddress &= ~USB_DIR_IN;
-               is_in = 0;
        }
        /* cope with automagic for some standard requests. */
        dev->req_std = (usb_ctrl->bRequestType & USB_TYPE_MASK)
                == USB_TYPE_STANDARD;
-       dev->req_config = 0;
+
        dev->req_pending = 1;
 
        /* Handle some SETUP packets ourselves */
        if (dev->req_std) {
                switch (usb_ctrl->bRequest) {
                case USB_REQ_SET_ADDRESS:
-               DEBUG_SETUP("%s: *** USB_REQ_SET_ADDRESS (%d)\n",
-                               __func__, usb_ctrl->wValue);
+               debug_cond(DEBUG_SETUP != 0,
+                          "%s: *** USB_REQ_SET_ADDRESS (%d)\n",
+                          __func__, usb_ctrl->wValue);
                        if (usb_ctrl->bRequestType
                                != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
                                break;
@@ -1319,34 +1350,37 @@ void s3c_ep0_setup(struct s3c_udc *dev)
                        return;
 
                case USB_REQ_SET_CONFIGURATION:
-                       DEBUG_SETUP("=====================================\n");
-                       DEBUG_SETUP("%s: USB_REQ_SET_CONFIGURATION (%d)\n",
-                                       __func__, usb_ctrl->wValue);
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "=====================================\n");
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "%s: USB_REQ_SET_CONFIGURATION (%d)\n",
+                                  __func__, usb_ctrl->wValue);
 
-                       if (usb_ctrl->bRequestType == USB_RECIP_DEVICE) {
+                       if (usb_ctrl->bRequestType == USB_RECIP_DEVICE)
                                reset_available = 1;
-                               dev->req_config = 1;
-                       }
+
                        break;
 
                case USB_REQ_GET_DESCRIPTOR:
-                       DEBUG_SETUP("%s: *** USB_REQ_GET_DESCRIPTOR\n",
-                                   __func__);
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "%s: *** USB_REQ_GET_DESCRIPTOR\n",
+                                  __func__);
                        break;
 
                case USB_REQ_SET_INTERFACE:
-                       DEBUG_SETUP("%s: *** USB_REQ_SET_INTERFACE (%d)\n",
-                                       __func__, usb_ctrl->wValue);
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "%s: *** USB_REQ_SET_INTERFACE (%d)\n",
+                                  __func__, usb_ctrl->wValue);
 
-                       if (usb_ctrl->bRequestType == USB_RECIP_INTERFACE) {
+                       if (usb_ctrl->bRequestType == USB_RECIP_INTERFACE)
                                reset_available = 1;
-                               dev->req_config = 1;
-                       }
+
                        break;
 
                case USB_REQ_GET_CONFIGURATION:
-                       DEBUG_SETUP("%s: *** USB_REQ_GET_CONFIGURATION\n",
-                                   __func__);
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "%s: *** USB_REQ_GET_CONFIGURATION\n",
+                                  __func__);
                        break;
 
                case USB_REQ_GET_STATUS:
@@ -1372,8 +1406,9 @@ void s3c_ep0_setup(struct s3c_udc *dev)
                        break;
 
                default:
-                       DEBUG_SETUP("%s: *** Default of usb_ctrl->bRequest=0x%x"
-                               "happened.\n", __func__, usb_ctrl->bRequest);
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "%s: *** Default of usb_ctrl->bRequest=0x%x"
+                                  "happened.\n", __func__, usb_ctrl->bRequest);
                        break;
                }
        }
@@ -1382,7 +1417,8 @@ void s3c_ep0_setup(struct s3c_udc *dev)
        if (likely(dev->driver)) {
                /* device-2-host (IN) or no data setup command,
                 * process immediately */
-               DEBUG_SETUP("%s:usb_ctrlreq will be passed to fsg_setup()\n",
+               debug_cond(DEBUG_SETUP != 0,
+                          "%s:usb_ctrlreq will be passed to fsg_setup()\n",
                            __func__);
 
                spin_unlock(&dev->lock);
@@ -1390,27 +1426,24 @@ void s3c_ep0_setup(struct s3c_udc *dev)
                spin_lock(&dev->lock);
 
                if (i < 0) {
-                       if (dev->req_config) {
-                               DEBUG_SETUP("\tconfig change 0x%02x fail %d?\n",
-                                       (u32)usb_ctrl->bRequest, i);
-                               return;
-                       }
-
                        /* setup processing failed, force stall */
                        s3c_udc_ep0_set_stall(ep);
                        dev->ep0state = WAIT_FOR_SETUP;
 
-                       DEBUG_SETUP("\tdev->driver->setup failed (%d),"
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "\tdev->driver->setup failed (%d),"
                                    " bRequest = %d\n",
                                i, usb_ctrl->bRequest);
 
 
                } else if (dev->req_pending) {
                        dev->req_pending = 0;
-                       DEBUG_SETUP("\tdev->req_pending...\n");
+                       debug_cond(DEBUG_SETUP != 0,
+                                  "\tdev->req_pending...\n");
                }
 
-               DEBUG_SETUP("\tep0state = %s\n", state_names[dev->ep0state]);
+               debug_cond(DEBUG_SETUP != 0,
+                          "\tep0state = %s\n", state_names[dev->ep0state]);
 
        }
 }
@@ -1421,18 +1454,21 @@ void s3c_ep0_setup(struct s3c_udc *dev)
 static void s3c_handle_ep0(struct s3c_udc *dev)
 {
        if (dev->ep0state == WAIT_FOR_SETUP) {
-               DEBUG_OUT_EP("%s: WAIT_FOR_SETUP\n", __func__);
+               debug_cond(DEBUG_OUT_EP != 0,
+                          "%s: WAIT_FOR_SETUP\n", __func__);
                s3c_ep0_setup(dev);
 
        } else {
-               DEBUG_OUT_EP("%s: strange state!!(state = %s)\n",
+               debug_cond(DEBUG_OUT_EP != 0,
+                          "%s: strange state!!(state = %s)\n",
                        __func__, state_names[dev->ep0state]);
        }
 }
 
 static void s3c_ep0_kick(struct s3c_udc *dev, struct s3c_ep *ep)
 {
-       DEBUG_EP0("%s: ep_is_in = %d\n", __func__, ep_is_in(ep));
+       debug_cond(DEBUG_EP0 != 0,
+                  "%s: ep_is_in = %d\n", __func__, ep_is_in(ep));
        if (ep_is_in(ep)) {
                dev->ep0state = DATA_STATE_XMIT;
                s3c_ep0_write(dev);