]> git.sur5r.net Git - u-boot/blobdiff - drivers/usb/gadget/designware_udc.c
SPDX: Convert all of our single license tags to Linux Kernel style
[u-boot] / drivers / usb / gadget / designware_udc.c
index aee44aa06479bfe0cf0bfe9b5d3dc4ee46ac457e..fa947dade6819f7b7c61110125395fb8e25d45cf 100644 (file)
@@ -1,27 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Based on drivers/usb/gadget/omap1510_udc.c
  * TI OMAP1510 USB bus interface driver
  *
  * (C) Copyright 2009
  * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
- *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * 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
  */
 
 #include <common.h>
@@ -30,6 +13,7 @@
 #include <usbdevice.h>
 #include "ep0.h"
 #include <usb/designware_udc.h>
+#include <usb/udc.h>
 #include <asm/arch/hardware.h>
 
 #define UDC_INIT_MDELAY                80      /* Device settle delay */
@@ -202,6 +186,7 @@ static int usbgetpckfromfifo(int epNum, u8 *bufp, u32 len)
        u32 i, nw, nb;
        u32 *wrdp;
        u8 *bytp;
+       u32 tmp[128];
 
        if (readl(&udc_regs_p->dev_stat) & DEV_STAT_RXFIFO_EMPTY)
                return -1;
@@ -209,7 +194,12 @@ static int usbgetpckfromfifo(int epNum, u8 *bufp, u32 len)
        nw = len / sizeof(u32);
        nb = len % sizeof(u32);
 
-       wrdp = (u32 *)bufp;
+       /* use tmp buf if bufp is not word aligned */
+       if ((int)bufp & 0x3)
+               wrdp = (u32 *)&tmp[0];
+       else
+               wrdp = (u32 *)bufp;
+
        for (i = 0; i < nw; i++) {
                writel(readl(fifo_ptr), wrdp);
                wrdp++;
@@ -223,6 +213,10 @@ static int usbgetpckfromfifo(int epNum, u8 *bufp, u32 len)
        }
        readl(&outep_regs_p[epNum].write_done);
 
+       /* copy back tmp buffer to bufp if bufp is not word aligned */
+       if ((int)bufp & 0x3)
+               memcpy(bufp, tmp, len);
+
        return 0;
 }
 
@@ -274,8 +268,8 @@ static void dw_write_noniso_tx_fifo(struct usb_endpoint_instance
                UDCDBGA("urb->buffer %p, buffer_length %d, actual_length %d",
                        urb->buffer, urb->buffer_length, urb->actual_length);
 
-               last = MIN(urb->actual_length - endpoint->sent,
-                          endpoint->tx_packetSize);
+               last = min_t(u32, urb->actual_length - endpoint->sent,
+                            endpoint->tx_packetSize);
 
                if (last) {
                        u8 *cp = urb->buffer + endpoint->sent;
@@ -290,7 +284,7 @@ static void dw_write_noniso_tx_fifo(struct usb_endpoint_instance
 
                        align = ((ulong)cp % sizeof(int));
                        if (align)
-                               last = MIN(last, sizeof(int) - align);
+                               last = min(last, sizeof(int) - align);
 
                        UDCDBGA("endpoint->sent %d, tx_packetSize %d, last %d",
                                endpoint->sent, endpoint->tx_packetSize, last);
@@ -497,16 +491,24 @@ static void dw_udc_epn_tx(int ep)
 {
        struct usb_endpoint_instance *endpoint = dw_find_ep(ep);
 
+       if (!endpoint)
+               return;
+
        /*
         * We need to transmit a terminating zero-length packet now if
         * we have sent all of the data in this URB and the transfer
         * size was an exact multiple of the packet size.
         */
-       if (endpoint && endpoint->tx_urb && endpoint->tx_urb->actual_length) {
-               if (endpoint->last == endpoint->tx_packetSize) {
-                       /* handle zero length packet here */
-                       writel(0x0, &inep_regs_p[ep].write_done);
-               }
+       if (endpoint->tx_urb &&
+           (endpoint->last == endpoint->tx_packetSize) &&
+           (endpoint->tx_urb->actual_length - endpoint->sent -
+            endpoint->last == 0)) {
+               /* handle zero length packet here */
+               writel(0x0, &inep_regs_p[ep].write_done);
+
+       }
+
+       if (endpoint->tx_urb && endpoint->tx_urb->actual_length) {
                /* retire the data that was just sent */
                usbd_tx_complete(endpoint);
                /*
@@ -548,8 +550,6 @@ int udc_init(void)
 
        readl(&plug_regs_p->plug_pending);
 
-       udc_disconnect();
-
        for (i = 0; i < UDC_INIT_MDELAY; i++)
                udelay(1000);
 
@@ -560,11 +560,15 @@ int udc_init(void)
        writel(~0x0, &udc_regs_p->dev_int_mask);
        writel(~0x0, &udc_regs_p->endp_int_mask);
 
+#ifndef CONFIG_USBD_HS
        writel(DEV_CONF_FS_SPEED | DEV_CONF_REMWAKEUP | DEV_CONF_SELFPOW |
-              /* Dev_Conf_SYNCFRAME | */
               DEV_CONF_PHYINT_16, &udc_regs_p->dev_conf);
+#else
+       writel(DEV_CONF_HS_SPEED | DEV_CONF_REMWAKEUP | DEV_CONF_SELFPOW |
+                       DEV_CONF_PHYINT_16, &udc_regs_p->dev_conf);
+#endif
 
-       writel(0x0, &udc_regs_p->dev_cntl);
+       writel(DEV_CNTL_SOFTDISCONNECT, &udc_regs_p->dev_cntl);
 
        /* Clear all interrupts pending */
        writel(DEV_INT_MSK, &udc_regs_p->dev_int);
@@ -572,6 +576,11 @@ int udc_init(void)
        return 0;
 }
 
+int is_usbd_high_speed(void)
+{
+       return (readl(&udc_regs_p->dev_stat) & DEV_STAT_ENUM) ? 0 : 1;
+}
+
 /*
  * udc_setup_ep - setup endpoint
  * Associate a physical endpoint with endpoint_instance
@@ -588,7 +597,10 @@ void udc_setup_ep(struct usb_device_instance *device,
        char *tt;
        u32 endp_intmask;
 
-       tt = getenv("usbtty");
+       if ((ep != 0) && (udc_device->device_state < STATE_ADDRESSED))
+               return;
+
+       tt = env_get("usbtty");
        if (!tt)
                tt = "generic";
 
@@ -647,9 +659,6 @@ void udc_setup_ep(struct usb_device_instance *device,
                writel(packet_size | ((buffer_size / sizeof(int)) << 16),
                       &out_p->endp_maxpacksize);
 
-               writel((packet_size << 19) | ENDP_EPTYPE_CNTL,
-                      &udc_regs_p->udc_endp_reg[ep_num]);
-
        } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
                /* Setup the IN endpoint */
                writel(0x0, &in_p->endp_status);
@@ -708,7 +717,17 @@ void udc_setup_ep(struct usb_device_instance *device,
 /* Turn on the USB connection by enabling the pullup resistor */
 void udc_connect(void)
 {
-       u32 plug_st;
+       u32 plug_st, dev_cntl;
+
+       dev_cntl = readl(&udc_regs_p->dev_cntl);
+       dev_cntl |= DEV_CNTL_SOFTDISCONNECT;
+       writel(dev_cntl, &udc_regs_p->dev_cntl);
+
+       udelay(1000);
+
+       dev_cntl = readl(&udc_regs_p->dev_cntl);
+       dev_cntl &= ~DEV_CNTL_SOFTDISCONNECT;
+       writel(dev_cntl, &udc_regs_p->dev_cntl);
 
        plug_st = readl(&plug_regs_p->plug_state);
        plug_st &= ~(PLUG_STATUS_PHY_RESET | PLUG_STATUS_PHY_MODE);
@@ -720,6 +739,8 @@ void udc_disconnect(void)
 {
        u32 plug_st;
 
+       writel(DEV_CNTL_SOFTDISCONNECT, &udc_regs_p->dev_cntl);
+
        plug_st = readl(&plug_regs_p->plug_state);
        plug_st |= (PLUG_STATUS_PHY_RESET | PLUG_STATUS_PHY_MODE);
        writel(plug_st, &plug_regs_p->plug_state);
@@ -775,7 +796,7 @@ void udc_startup_events(struct usb_device_instance *device)
 /*
  * Plug detection interrupt handling
  */
-void dw_udc_plug_irq(void)
+static void dw_udc_plug_irq(void)
 {
        if (readl(&plug_regs_p->plug_state) & PLUG_STATUS_ATTACHED) {
                /*
@@ -789,11 +810,6 @@ void dw_udc_plug_irq(void)
                UDCDBG("device attached and powered");
                udc_state_transition(udc_device->device_state, STATE_POWERED);
        } else {
-               /*
-                * USB cable detached
-                * Reset the PHY and switch the mode.
-                */
-               udc_disconnect();
                writel(~0x0, &udc_regs_p->dev_int_mask);
 
                UDCDBG("device detached or unpowered");
@@ -804,18 +820,23 @@ void dw_udc_plug_irq(void)
 /*
  * Device interrupt handling
  */
-void dw_udc_dev_irq(void)
+static void dw_udc_dev_irq(void)
 {
        if (readl(&udc_regs_p->dev_int) & DEV_INT_USBRESET) {
                writel(~0x0, &udc_regs_p->endp_int_mask);
 
-               udc_connect();
-
                writel(readl(&inep_regs_p[0].endp_cntl) | ENDP_CNTL_FLUSH,
                       &inep_regs_p[0].endp_cntl);
 
                writel(DEV_INT_USBRESET, &udc_regs_p->dev_int);
 
+               /*
+                * This endpoint0 specific register can be programmed only
+                * after the phy clock is initialized
+                */
+               writel((EP0_MAX_PACKET_SIZE << 19) | ENDP_EPTYPE_CNTL,
+                               &udc_regs_p->udc_endp_reg[0]);
+
                UDCDBG("device reset in progess");
                udc_state_transition(udc_device->device_state, STATE_DEFAULT);
        }
@@ -869,7 +890,7 @@ void dw_udc_dev_irq(void)
 /*
  * Endpoint interrupt handling
  */
-void dw_udc_endpoint_irq(void)
+static void dw_udc_endpoint_irq(void)
 {
        while (readl(&udc_regs_p->endp_int) & ENDP0_INT_CTRLOUT) {
 
@@ -902,7 +923,7 @@ void dw_udc_endpoint_irq(void)
                writel(ENDP0_INT_CTRLIN, &udc_regs_p->endp_int);
        }
 
-       while (readl(&udc_regs_p->endp_int) & ENDP_INT_NONISOOUT_MSK) {
+       if (readl(&udc_regs_p->endp_int) & ENDP_INT_NONISOOUT_MSK) {
                u32 epnum = 0;
                u32 ep_int = readl(&udc_regs_p->endp_int) &
                    ENDP_INT_NONISOOUT_MSK;