]> git.sur5r.net Git - u-boot/blobdiff - drivers/usb/gadget/s3c_udc_otg.c
usb: dwc3: add dwc3 folder from linux kernel to u-boot
[u-boot] / drivers / usb / gadget / s3c_udc_otg.c
index 901fac982a4153fafbac42bede8cf1d89deaf7ca..7653f03949a114c1d32274a6cd1ff41b32b48c93 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+
  */
-
+#undef DEBUG
 #include <common.h>
 #include <asm/errno.h>
 #include <linux/list.h>
 #include <asm/io.h>
 
 #include <asm/mach-types.h>
-#include <asm/arch/gpio.h>
 
 #include "regs-otg.h"
-#include <usb/s3c_udc.h>
 #include <usb/lin_gadget_compat.h>
 
 /***********************************************************/
 
 #define OTG_DMA_MODE           1
 
-#undef DEBUG_S3C_UDC_SETUP
-#undef DEBUG_S3C_UDC_EP0
-#undef DEBUG_S3C_UDC_ISR
-#undef DEBUG_S3C_UDC_OUT_EP
-#undef DEBUG_S3C_UDC_IN_EP
-#undef DEBUG_S3C_UDC
-
-/* #define DEBUG_S3C_UDC_SETUP */
-/* #define DEBUG_S3C_UDC_EP0 */
-/* #define DEBUG_S3C_UDC_ISR */
-/* #define DEBUG_S3C_UDC_OUT_EP */
-/* #define DEBUG_S3C_UDC_IN_EP */
-/* #define DEBUG_S3C_UDC */
+#define DEBUG_SETUP 0
+#define DEBUG_EP0 0
+#define DEBUG_ISR 0
+#define DEBUG_OUT_EP 0
+#define DEBUG_IN_EP 0
 
 #include <usb/s3c_udc.h>
 
 #define EP0_CON                0
 #define EP_MASK                0xF
 
-#if defined(DEBUG_S3C_UDC_SETUP) || defined(DEBUG_S3C_UDC_ISR)   \
-       || defined(DEBUG_S3C_UDC_OUT_EP)
 static char *state_names[] = {
        "WAIT_FOR_SETUP",
        "DATA_STATE_XMIT",
@@ -86,7 +61,6 @@ static char *state_names[] = {
        "WAIT_FOR_IN_COMPLETE",
        "WAIT_FOR_NULL_COMPLETE",
 };
-#endif
 
 #define DRIVER_DESC "S3C HS USB OTG Device Driver, (c) Samsung Electronics"
 #define DRIVER_VERSION "15 March 2009"
@@ -130,12 +104,25 @@ static void stop_activity(struct s3c_udc *dev,
                          struct usb_gadget_driver *driver);
 static int udc_enable(struct s3c_udc *dev);
 static void udc_set_address(struct s3c_udc *dev, unsigned char address);
-static void reconfig_usbd(void);
+static void reconfig_usbd(struct s3c_udc *dev);
 static void set_max_pktsize(struct s3c_udc *dev, enum usb_device_speed speed);
 static void nuke(struct s3c_ep *ep, int status);
 static int s3c_udc_set_halt(struct usb_ep *_ep, int value);
 static void s3c_udc_set_nak(struct s3c_ep *ep);
 
+void set_udc_gadget_private_data(void *p)
+{
+       debug_cond(DEBUG_SETUP != 0,
+                  "%s: the_controller: 0x%p, p: 0x%p\n", __func__,
+                  the_controller, p);
+       the_controller->gadget.dev.device_data = p;
+}
+
+void *get_udc_gadget_private_data(struct usb_gadget *gadget)
+{
+       return gadget->dev.device_data;
+}
+
 static struct usb_ep_ops s3c_ep_ops = {
        .enable = s3c_ep_enable,
        .disable = s3c_ep_disable,
@@ -158,58 +145,14 @@ static struct usb_ep_ops s3c_ep_ops = {
 
 void __iomem           *regs_otg;
 struct s3c_usbotg_reg *reg;
-struct s3c_usbotg_phy *phy;
-static unsigned int usb_phy_ctrl;
 
-void otg_phy_init(struct s3c_udc *dev)
+bool dfu_usb_get_reset(void)
 {
-       dev->pdata->phy_control(1);
-
-       /*USB PHY0 Enable */
-       printf("USB PHY0 Enable\n");
-
-       /* Enable PHY */
-       writel(readl(usb_phy_ctrl) | USB_PHY_CTRL_EN0, usb_phy_ctrl);
-
-       if (dev->pdata->usb_flags == PHY0_SLEEP) /* C210 Universal */
-               writel((readl(&phy->phypwr)
-                       &~(PHY_0_SLEEP | OTG_DISABLE_0 | ANALOG_PWRDOWN)
-                       &~FORCE_SUSPEND_0), &phy->phypwr);
-       else /* C110 GONI */
-               writel((readl(&phy->phypwr) &~(OTG_DISABLE_0 | ANALOG_PWRDOWN)
-                       &~FORCE_SUSPEND_0), &phy->phypwr);
-
-       writel((readl(&phy->phyclk) &~(ID_PULLUP0 | COMMON_ON_N0)) |
-              CLK_SEL_24MHZ, &phy->phyclk); /* PLL 24Mhz */
-
-       writel((readl(&phy->rstcon) &~(LINK_SW_RST | PHYLNK_SW_RST))
-              | PHY_SW_RST0, &phy->rstcon);
-       udelay(10);
-       writel(readl(&phy->rstcon)
-              &~(PHY_SW_RST0 | LINK_SW_RST | PHYLNK_SW_RST), &phy->rstcon);
-       udelay(10);
+       return !!(readl(&reg->gintsts) & INT_RESET);
 }
 
-void otg_phy_off(struct s3c_udc *dev)
-{
-       /* reset controller just in case */
-       writel(PHY_SW_RST0, &phy->rstcon);
-       udelay(20);
-       writel(readl(&phy->phypwr) &~PHY_SW_RST0, &phy->rstcon);
-       udelay(20);
-
-       writel(readl(&phy->phypwr) | OTG_DISABLE_0 | ANALOG_PWRDOWN
-              | FORCE_SUSPEND_0, &phy->phypwr);
-
-       writel(readl(usb_phy_ctrl) &~USB_PHY_CTRL_EN0, usb_phy_ctrl);
-
-       writel((readl(&phy->phyclk) & ~(ID_PULLUP0 | COMMON_ON_N0)),
-             &phy->phyclk);
-
-       udelay(10000);
-
-       dev->pdata->phy_control(0);
-}
+__weak void otg_phy_init(struct s3c_udc *dev) {}
+__weak void otg_phy_off(struct s3c_udc *dev) {}
 
 /***********************************************************/
 
@@ -220,7 +163,7 @@ void otg_phy_off(struct s3c_udc *dev)
  */
 static void udc_disable(struct s3c_udc *dev)
 {
-       DEBUG_SETUP("%s: %p\n", __func__, dev);
+       debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
 
        udc_set_address(dev, 0);
 
@@ -238,7 +181,7 @@ static void udc_reinit(struct s3c_udc *dev)
 {
        unsigned int i;
 
-       DEBUG_SETUP("%s: %p\n", __func__, dev);
+       debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
 
        /* device/ep0 records init */
        INIT_LIST_HEAD(&dev->gadget.ep_list);
@@ -269,12 +212,13 @@ static void udc_reinit(struct s3c_udc *dev)
  */
 static int udc_enable(struct s3c_udc *dev)
 {
-       DEBUG_SETUP("%s: %p\n", __func__, dev);
+       debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
 
        otg_phy_init(dev);
-       reconfig_usbd();
+       reconfig_usbd(dev);
 
-       DEBUG_SETUP("S3C USB 2.0 OTG Controller Core Initialized : 0x%x\n",
+       debug_cond(DEBUG_SETUP != 0,
+                  "S3C USB 2.0 OTG Controller Core Initialized : 0x%x\n",
                    readl(&reg->gintmsk));
 
        dev->gadget.speed = USB_SPEED_UNKNOWN;
@@ -289,9 +233,9 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
 {
        struct s3c_udc *dev = the_controller;
        int retval = 0;
-       unsigned long flags;
+       unsigned long flags = 0;
 
-       DEBUG_SETUP("%s: %s\n", __func__, "no name");
+       debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
 
        if (!driver
            || (driver->speed != USB_SPEED_FULL
@@ -315,7 +259,8 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
 
        retval = driver->bind(&dev->gadget);
        if (retval) {
-               DEBUG_SETUP("%s: bind to driver --> error %d\n",
+               debug_cond(DEBUG_SETUP != 0,
+                          "%s: bind to driver --> error %d\n",
                            dev->gadget.name, retval);
                dev->driver = 0;
                return retval;
@@ -323,7 +268,8 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
 
        enable_irq(IRQ_OTG);
 
-       DEBUG_SETUP("Registered gadget driver %s\n", dev->gadget.name);
+       debug_cond(DEBUG_SETUP != 0,
+                  "Registered gadget driver %s\n", dev->gadget.name);
        udc_enable(dev);
 
        return 0;
@@ -335,7 +281,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
 {
        struct s3c_udc *dev = the_controller;
-       unsigned long flags;
+       unsigned long flags = 0;
 
        if (!dev)
                return -ENODEV;
@@ -362,7 +308,7 @@ static void done(struct s3c_ep *ep, struct s3c_request *req, int status)
 {
        unsigned int stopped = ep->stopped;
 
-       DEBUG("%s: %s %p, req = %p, stopped = %d\n",
+       debug("%s: %s %p, req = %p, stopped = %d\n",
              __func__, ep->ep.name, ep, &req->req, stopped);
 
        list_del_init(&req->queue);
@@ -373,7 +319,7 @@ static void done(struct s3c_ep *ep, struct s3c_request *req, int status)
                status = req->req.status;
 
        if (status && status != -ESHUTDOWN) {
-               DEBUG("complete %s req %p stat %d len %u/%u\n",
+               debug("complete %s req %p stat %d len %u/%u\n",
                      ep->ep.name, &req->req, status,
                      req->req.actual, req->req.length);
        }
@@ -381,7 +327,7 @@ static void done(struct s3c_ep *ep, struct s3c_request *req, int status)
        /* don't modify queue heads during completion callback */
        ep->stopped = 1;
 
-#ifdef DEBUG_S3C_UDC
+#ifdef DEBUG
        printf("calling complete callback\n");
        {
                int i, len = req->req.length;
@@ -401,7 +347,7 @@ static void done(struct s3c_ep *ep, struct s3c_request *req, int status)
        req->req.complete(&ep->ep, &req->req);
        spin_lock(&ep->dev->lock);
 
-       DEBUG("callback completed\n");
+       debug("callback completed\n");
 
        ep->stopped = stopped;
 }
@@ -413,7 +359,7 @@ static void nuke(struct s3c_ep *ep, int status)
 {
        struct s3c_request *req;
 
-       DEBUG("%s: %s %p\n", __func__, ep->ep.name, ep);
+       debug("%s: %s %p\n", __func__, ep->ep.name, ep);
 
        /* called with irqs blocked */
        while (!list_empty(&ep->queue)) {
@@ -450,15 +396,17 @@ static void stop_activity(struct s3c_udc *dev,
        udc_reinit(dev);
 }
 
-static void reconfig_usbd(void)
+static void reconfig_usbd(struct s3c_udc *dev)
 {
        /* 2. Soft-reset OTG Core and then unreset again. */
        int i;
        unsigned int uTemp = writel(CORE_SOFT_RESET, &reg->grstctl);
+       uint32_t dflt_gusbcfg;
 
-       DEBUG(2, "Reseting OTG controller\n");
+       debug("Reseting OTG controller\n");
 
-       writel(0<<15            /* PHY Low Power Clock sel*/
+       dflt_gusbcfg =
+               0<<15           /* PHY Low Power Clock sel*/
                |1<<14          /* Non-Periodic TxFIFO Rewind Enable*/
                |0x5<<10        /* Turnaround time*/
                |0<<9 | 0<<8    /* [0:HNP disable,1:HNP enable][ 0:SRP disable*/
@@ -467,8 +415,12 @@ static void reconfig_usbd(void)
                |0<<6           /* 0: high speed utmi+, 1: full speed serial*/
                |0<<4           /* 0: utmi+, 1:ulpi*/
                |1<<3           /* phy i/f  0:8bit, 1:16bit*/
-               |0x7<<0,        /* HS/FS Timeout**/
-               &reg->gusbcfg);
+               |0x7<<0;        /* HS/FS Timeout**/
+
+       if (dev->pdata->usb_gusbcfg)
+               dflt_gusbcfg = dev->pdata->usb_gusbcfg;
+
+       writel(dflt_gusbcfg, &reg->gusbcfg);
 
        /* 3. Put the OTG device core in the disconnected state.*/
        uTemp = readl(&reg->dctl);
@@ -526,13 +478,13 @@ static void reconfig_usbd(void)
        /* Flush the RX FIFO */
        writel(RX_FIFO_FLUSH, &reg->grstctl);
        while (readl(&reg->grstctl) & RX_FIFO_FLUSH)
-               DEBUG("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __func__);
+               debug("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __func__);
 
        /* Flush all the Tx FIFO's */
        writel(TX_FIFO_FLUSH_ALL, &reg->grstctl);
        writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, &reg->grstctl);
        while (readl(&reg->grstctl) & TX_FIFO_FLUSH)
-               DEBUG("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __func__);
+               debug("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __func__);
 
        /* 13. Clear NAK bit of EP0, EP1, EP2*/
        /* For Slave mode*/
@@ -579,9 +531,9 @@ static int s3c_ep_enable(struct usb_ep *_ep,
 {
        struct s3c_ep *ep;
        struct s3c_udc *dev;
-       unsigned long flags;
+       unsigned long flags = 0;
 
-       DEBUG("%s: %p\n", __func__, _ep);
+       debug("%s: %p\n", __func__, _ep);
 
        ep = container_of(_ep, struct s3c_ep, ep);
        if (!_ep || !desc || ep->desc || _ep->name == ep0name
@@ -590,7 +542,7 @@ static int s3c_ep_enable(struct usb_ep *_ep,
            || ep_maxpacket(ep) <
            le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
 
-               DEBUG("%s: bad ep or descriptor\n", __func__);
+               debug("%s: bad ep or descriptor\n", __func__);
                return -EINVAL;
        }
 
@@ -599,7 +551,7 @@ static int s3c_ep_enable(struct usb_ep *_ep,
            && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
            && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
 
-               DEBUG("%s: %s type mismatch\n", __func__, _ep->name);
+               debug("%s: %s type mismatch\n", __func__, _ep->name);
                return -EINVAL;
        }
 
@@ -608,14 +560,14 @@ static int s3c_ep_enable(struct usb_ep *_ep,
             && le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)) !=
             ep_maxpacket(ep)) || !get_unaligned(&desc->wMaxPacketSize)) {
 
-               DEBUG("%s: bad %s maxpacket\n", __func__, _ep->name);
+               debug("%s: bad %s maxpacket\n", __func__, _ep->name);
                return -ERANGE;
        }
 
        dev = ep->dev;
        if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
 
-               DEBUG("%s: bogus device state\n", __func__);
+               debug("%s: bogus device state\n", __func__);
                return -ESHUTDOWN;
        }
 
@@ -632,7 +584,7 @@ static int s3c_ep_enable(struct usb_ep *_ep,
        s3c_udc_ep_activate(ep);
        spin_unlock_irqrestore(&ep->dev->lock, flags);
 
-       DEBUG("%s: enabled %s, stopped = %d, maxpacket = %d\n",
+       debug("%s: enabled %s, stopped = %d, maxpacket = %d\n",
              __func__, _ep->name, ep->stopped, ep->ep.maxpacket);
        return 0;
 }
@@ -643,13 +595,13 @@ static int s3c_ep_enable(struct usb_ep *_ep,
 static int s3c_ep_disable(struct usb_ep *_ep)
 {
        struct s3c_ep *ep;
-       unsigned long flags;
+       unsigned long flags = 0;
 
-       DEBUG("%s: %p\n", __func__, _ep);
+       debug("%s: %p\n", __func__, _ep);
 
        ep = container_of(_ep, struct s3c_ep, ep);
        if (!_ep || !ep->desc) {
-               DEBUG("%s: %s not enabled\n", __func__,
+               debug("%s: %s not enabled\n", __func__,
                      _ep ? ep->ep.name : NULL);
                return -EINVAL;
        }
@@ -664,7 +616,7 @@ static int s3c_ep_disable(struct usb_ep *_ep)
 
        spin_unlock_irqrestore(&ep->dev->lock, flags);
 
-       DEBUG("%s: disabled %s\n", __func__, _ep->name);
+       debug("%s: disabled %s\n", __func__, _ep->name);
        return 0;
 }
 
@@ -673,9 +625,9 @@ static struct usb_request *s3c_alloc_request(struct usb_ep *ep,
 {
        struct s3c_request *req;
 
-       DEBUG("%s: %s %p\n", __func__, ep->name, ep);
+       debug("%s: %s %p\n", __func__, ep->name, ep);
 
-       req = kmalloc(sizeof *req, gfp_flags);
+       req = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*req));
        if (!req)
                return 0;
 
@@ -689,7 +641,7 @@ static void s3c_free_request(struct usb_ep *ep, struct usb_request *_req)
 {
        struct s3c_request *req;
 
-       DEBUG("%s: %p\n", __func__, ep);
+       debug("%s: %p\n", __func__, ep);
 
        req = container_of(_req, struct s3c_request, req);
        WARN_ON(!list_empty(&req->queue));
@@ -701,9 +653,9 @@ static int s3c_dequeue(struct usb_ep *_ep, struct usb_request *_req)
 {
        struct s3c_ep *ep;
        struct s3c_request *req;
-       unsigned long flags;
+       unsigned long flags = 0;
 
-       DEBUG("%s: %p\n", __func__, _ep);
+       debug("%s: %p\n", __func__, _ep);
 
        ep = container_of(_ep, struct s3c_ep, ep);
        if (!_ep || ep->ep.name == ep0name)
@@ -737,11 +689,11 @@ static int s3c_fifo_status(struct usb_ep *_ep)
 
        ep = container_of(_ep, struct s3c_ep, ep);
        if (!_ep) {
-               DEBUG("%s: bad ep\n", __func__);
+               debug("%s: bad ep\n", __func__);
                return -ENODEV;
        }
 
-       DEBUG("%s: %d\n", __func__, ep_index(ep));
+       debug("%s: %d\n", __func__, ep_index(ep));
 
        /* LPD can't report unclaimed bytes from IN fifos */
        if (ep_is_in(ep))
@@ -759,11 +711,11 @@ static void s3c_fifo_flush(struct usb_ep *_ep)
 
        ep = container_of(_ep, struct s3c_ep, ep);
        if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
-               DEBUG("%s: bad ep\n", __func__);
+               debug("%s: bad ep\n", __func__);
                return;
        }
 
-       DEBUG("%s: %d\n", __func__, ep_index(ep));
+       debug("%s: %d\n", __func__, ep_index(ep));
 }
 
 static const struct usb_gadget_ops s3c_udc_ops = {
@@ -847,15 +799,13 @@ static struct s3c_udc memory = {
 int s3c_udc_probe(struct s3c_plat_otg_data *pdata)
 {
        struct s3c_udc *dev = &memory;
-       int retval = 0, i;
+       int retval = 0;
 
-       DEBUG("%s: %p\n", __func__, pdata);
+       debug("%s: %p\n", __func__, pdata);
 
        dev->pdata = pdata;
 
-       phy = (struct s3c_usbotg_phy *)pdata->regs_phy;
        reg = (struct s3c_usbotg_reg *)pdata->regs_otg;
-       usb_phy_ctrl = pdata->usb_phy_ctrl;
 
        /* regs_otg = (void *)pdata->regs_otg; */
 
@@ -868,15 +818,15 @@ int s3c_udc_probe(struct s3c_plat_otg_data *pdata)
 
        the_controller = dev;
 
-       for (i = 0; i < S3C_MAX_ENDPOINTS+1; i++) {
-               dev->dma_buf[i] = kmalloc(DMA_BUFFER_SIZE, GFP_KERNEL);
-               dev->dma_addr[i] = (dma_addr_t) dev->dma_buf[i];
-               invalidate_dcache_range((unsigned long) dev->dma_buf[i],
-                                       (unsigned long) (dev->dma_buf[i]
-                                                        + DMA_BUFFER_SIZE));
+       usb_ctrl = memalign(CONFIG_SYS_CACHELINE_SIZE,
+                           ROUND(sizeof(struct usb_ctrlrequest),
+                                 CONFIG_SYS_CACHELINE_SIZE));
+       if (!usb_ctrl) {
+               error("No memory available for UDC!\n");
+               return -ENOMEM;
        }
-       usb_ctrl = dev->dma_buf[0];
-       usb_ctrl_dma_addr = dev->dma_addr[0];
+
+       usb_ctrl_dma_addr = (dma_addr_t) usb_ctrl;
 
        udc_reinit(dev);