Merge git://git.denx.de/u-boot-dm
[oweals/u-boot.git] / drivers / usb / host / ehci-hcd.c
index 65428924c0d08a63ff5122ef301f5cca29ad95a1..3a0d32ee2ba1f0e3c9d49500e4570103e0f70487 100644 (file)
@@ -5,20 +5,7 @@
  *
  * All rights reserved.
  *
- * 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 version 2 of
- * the License.
- *
- * 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
  */
 #include <common.h>
 #include <dm.h>
@@ -321,7 +308,7 @@ static void ehci_update_endpt2_dev_n_port(struct usb_device *udev,
                struct udevice *dev = parent;
 
                if (device_get_uclass_id(dev->parent) != UCLASS_USB_HUB) {
-                       printf("ehci: Error cannot find high speed parent of usb-1 device\n");
+                       printf("ehci: Error cannot find high-speed parent of usb-1 device\n");
                        return;
                }
 
@@ -1214,6 +1201,7 @@ static int _ehci_submit_control_msg(struct usb_device *dev, unsigned long pipe,
 
 struct int_queue {
        int elementsize;
+       unsigned long pipe;
        struct QH *first;
        struct QH *current;
        struct QH *last;
@@ -1269,7 +1257,7 @@ static struct int_queue *_ehci_create_int_queue(struct usb_device *dev,
 {
        struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
        struct int_queue *result = NULL;
-       int i;
+       uint32_t i, toggle;
 
        /*
         * Interrupt transfers requiring several transactions are not supported
@@ -1309,6 +1297,7 @@ static struct int_queue *_ehci_create_int_queue(struct usb_device *dev,
                goto fail1;
        }
        result->elementsize = elementsize;
+       result->pipe = pipe;
        result->first = memalign(USB_DMA_MINALIGN,
                                 sizeof(struct QH) * queuesize);
        if (!result->first) {
@@ -1326,6 +1315,8 @@ static struct int_queue *_ehci_create_int_queue(struct usb_device *dev,
        memset(result->first, 0, sizeof(struct QH) * queuesize);
        memset(result->tds, 0, sizeof(struct qTD) * queuesize);
 
+       toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
+
        for (i = 0; i < queuesize; i++) {
                struct QH *qh = result->first + i;
                struct qTD *td = result->tds + i;
@@ -1357,7 +1348,9 @@ static struct int_queue *_ehci_create_int_queue(struct usb_device *dev,
                td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
                debug("communication direction is '%s'\n",
                      usb_pipein(pipe) ? "in" : "out");
-               td->qt_token = cpu_to_hc32((elementsize << 16) |
+               td->qt_token = cpu_to_hc32(
+                       QT_TOKEN_DT(toggle) |
+                       (elementsize << 16) |
                        ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
                        0x80); /* active */
                td->qt_buffer[0] =
@@ -1372,6 +1365,7 @@ static struct int_queue *_ehci_create_int_queue(struct usb_device *dev,
                    cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
 
                *buf = buffer + i * elementsize;
+               toggle ^= 1;
        }
 
        flush_dcache_range((unsigned long)buffer,
@@ -1426,6 +1420,8 @@ static void *_ehci_poll_int_queue(struct usb_device *dev,
 {
        struct QH *cur = queue->current;
        struct qTD *cur_td;
+       uint32_t token, toggle;
+       unsigned long pipe = queue->pipe;
 
        /* depleted queue */
        if (cur == NULL) {
@@ -1436,12 +1432,15 @@ static void *_ehci_poll_int_queue(struct usb_device *dev,
        cur_td = &queue->tds[queue->current - queue->first];
        invalidate_dcache_range((unsigned long)cur_td,
                                ALIGN_END_ADDR(struct qTD, cur_td, 1));
-       if (QT_TOKEN_GET_STATUS(hc32_to_cpu(cur_td->qt_token)) &
-                       QT_TOKEN_STATUS_ACTIVE) {
-               debug("Exit poll_int_queue with no completed intr transfer. token is %x\n",
-                     hc32_to_cpu(cur_td->qt_token));
+       token = hc32_to_cpu(cur_td->qt_token);
+       if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE) {
+               debug("Exit poll_int_queue with no completed intr transfer. token is %x\n", token);
                return NULL;
        }
+
+       toggle = QT_TOKEN_GET_DT(token);
+       usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), toggle);
+
        if (!(cur->qh_link & QH_LINK_TERMINATE))
                queue->current++;
        else
@@ -1452,7 +1451,7 @@ static void *_ehci_poll_int_queue(struct usb_device *dev,
                                               queue->elementsize));
 
        debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
-             hc32_to_cpu(cur_td->qt_token), cur, queue->first);
+             token, cur, queue->first);
        return cur->buffer;
 }
 
@@ -1605,6 +1604,29 @@ static int ehci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
        return _ehci_submit_int_msg(udev, pipe, buffer, length, interval);
 }
 
+static struct int_queue *ehci_create_int_queue(struct udevice *dev,
+               struct usb_device *udev, unsigned long pipe, int queuesize,
+               int elementsize, void *buffer, int interval)
+{
+       debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
+       return _ehci_create_int_queue(udev, pipe, queuesize, elementsize,
+                                     buffer, interval);
+}
+
+static void *ehci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
+                                struct int_queue *queue)
+{
+       debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
+       return _ehci_poll_int_queue(udev, queue);
+}
+
+static int ehci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
+                                 struct int_queue *queue)
+{
+       debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
+       return _ehci_destroy_int_queue(udev, queue);
+}
+
 int ehci_register(struct udevice *dev, struct ehci_hccr *hccr,
                  struct ehci_hcor *hcor, const struct ehci_ops *ops,
                  uint tweaks, enum usb_init_type init)
@@ -1653,6 +1675,9 @@ struct dm_usb_ops ehci_usb_ops = {
        .control = ehci_submit_control_msg,
        .bulk = ehci_submit_bulk_msg,
        .interrupt = ehci_submit_int_msg,
+       .create_int_queue = ehci_create_int_queue,
+       .poll_int_queue = ehci_poll_int_queue,
+       .destroy_int_queue = ehci_destroy_int_queue,
 };
 
 #endif