]> git.sur5r.net Git - u-boot/blobdiff - drivers/usb/host/xhci-mem.c
SPDX: Convert all of our single license tags to Linux Kernel style
[u-boot] / drivers / usb / host / xhci-mem.c
index 89908e8a8062c5fc3348dfdd4ac81f25d2c3ca04..da5dbd94edd074891419dc5fd9f5b87d4c03fc4d 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * USB HOST XHCI Controller stack
  *
  * Copyright (C) 2013 Samsung Electronics Co.Ltd
  * Authors: Vivek Gautam <gautam.vivek@samsung.com>
  *         Vikas Sajjan <vikas.sajjan@samsung.com>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
+#include <dm.h>
 #include <asm/byteorder.h>
 #include <usb.h>
 #include <malloc.h>
 #include <asm/cache.h>
-#include <asm-generic/errno.h>
+#include <linux/errno.h>
 
 #include "xhci.h"
 
@@ -31,7 +31,7 @@
  * @param len  the length of the cache line to be flushed
  * @return none
  */
-void xhci_flush_cache(uint32_t addr, u32 len)
+void xhci_flush_cache(uintptr_t addr, u32 len)
 {
        BUG_ON((void *)addr == NULL || len == 0);
 
@@ -46,7 +46,7 @@ void xhci_flush_cache(uint32_t addr, u32 len)
  * @param len  the length of the cache line to be invalidated
  * @return none
  */
-void xhci_inval_cache(uint32_t addr, u32 len)
+void xhci_inval_cache(uintptr_t addr, u32 len)
 {
        BUG_ON((void *)addr == NULL || len == 0);
 
@@ -94,6 +94,25 @@ static void xhci_ring_free(struct xhci_ring *ring)
        free(ring);
 }
 
+/**
+ * Free the scratchpad buffer array and scratchpad buffers
+ *
+ * @ctrl       host controller data structure
+ * @return     none
+ */
+static void xhci_scratchpad_free(struct xhci_ctrl *ctrl)
+{
+       if (!ctrl->scratchpad)
+               return;
+
+       ctrl->dcbaa->dev_context_ptrs[0] = 0;
+
+       free((void *)(uintptr_t)ctrl->scratchpad->sp_array[0]);
+       free(ctrl->scratchpad->sp_array);
+       free(ctrl->scratchpad);
+       ctrl->scratchpad = NULL;
+}
+
 /**
  * frees the "xhci_container_ctx" pointer passed
  *
@@ -154,6 +173,7 @@ void xhci_cleanup(struct xhci_ctrl *ctrl)
 {
        xhci_ring_free(ctrl->event_ring);
        xhci_ring_free(ctrl->cmd_ring);
+       xhci_scratchpad_free(ctrl);
        xhci_free_virt_devices(ctrl);
        free(ctrl->erst.entries);
        free(ctrl->dcbaa);
@@ -175,7 +195,7 @@ static void *xhci_malloc(unsigned int size)
        BUG_ON(!ptr);
        memset(ptr, '\0', size);
 
-       xhci_flush_cache((uint32_t)ptr, size);
+       xhci_flush_cache((uintptr_t)ptr, size);
 
        return ptr;
 }
@@ -318,6 +338,70 @@ struct xhci_ring *xhci_ring_alloc(unsigned int num_segs, bool link_trbs)
        return ring;
 }
 
+/**
+ * Set up the scratchpad buffer array and scratchpad buffers
+ *
+ * @ctrl       host controller data structure
+ * @return     -ENOMEM if buffer allocation fails, 0 on success
+ */
+static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl)
+{
+       struct xhci_hccr *hccr = ctrl->hccr;
+       struct xhci_hcor *hcor = ctrl->hcor;
+       struct xhci_scratchpad *scratchpad;
+       int num_sp;
+       uint32_t page_size;
+       void *buf;
+       int i;
+
+       num_sp = HCS_MAX_SCRATCHPAD(xhci_readl(&hccr->cr_hcsparams2));
+       if (!num_sp)
+               return 0;
+
+       scratchpad = malloc(sizeof(*scratchpad));
+       if (!scratchpad)
+               goto fail_sp;
+       ctrl->scratchpad = scratchpad;
+
+       scratchpad->sp_array = xhci_malloc(num_sp * sizeof(u64));
+       if (!scratchpad->sp_array)
+               goto fail_sp2;
+       ctrl->dcbaa->dev_context_ptrs[0] =
+               cpu_to_le64((uintptr_t)scratchpad->sp_array);
+
+       page_size = xhci_readl(&hcor->or_pagesize) & 0xffff;
+       for (i = 0; i < 16; i++) {
+               if ((0x1 & page_size) != 0)
+                       break;
+               page_size = page_size >> 1;
+       }
+       BUG_ON(i == 16);
+
+       page_size = 1 << (i + 12);
+       buf = memalign(page_size, num_sp * page_size);
+       if (!buf)
+               goto fail_sp3;
+       memset(buf, '\0', num_sp * page_size);
+       xhci_flush_cache((uintptr_t)buf, num_sp * page_size);
+
+       for (i = 0; i < num_sp; i++) {
+               uintptr_t ptr = (uintptr_t)buf + i * page_size;
+               scratchpad->sp_array[i] = cpu_to_le64(ptr);
+       }
+
+       return 0;
+
+fail_sp3:
+       free(scratchpad->sp_array);
+
+fail_sp2:
+       free(scratchpad);
+       ctrl->scratchpad = NULL;
+
+fail_sp:
+       return -ENOMEM;
+}
+
 /**
  * Allocates the Container context
  *
@@ -352,12 +436,10 @@ static struct xhci_container_ctx
  * @param udev pointer to USB deivce structure
  * @return 0 on success else -1 on failure
  */
-int xhci_alloc_virt_device(struct usb_device *udev)
+int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
 {
        u64 byte_64 = 0;
-       unsigned int slot_id = udev->slot_id;
        struct xhci_virt_device *virt_dev;
-       struct xhci_ctrl *ctrl = udev->controller;
 
        /* Slot ID 0 is reserved */
        if (ctrl->devs[slot_id]) {
@@ -400,8 +482,8 @@ int xhci_alloc_virt_device(struct usb_device *udev)
        /* Point to output device context in dcbaa. */
        ctrl->dcbaa->dev_context_ptrs[slot_id] = byte_64;
 
-       xhci_flush_cache((uint32_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
-                                                       sizeof(__le64));
+       xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
+                        sizeof(__le64));
        return 0;
 }
 
@@ -478,8 +560,8 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,
                entry->rsvd = 0;
                seg = seg->next;
        }
-       xhci_flush_cache((uint32_t)ctrl->erst.entries,
-                       ERST_NUM_SEGS * sizeof(struct xhci_erst_entry));
+       xhci_flush_cache((uintptr_t)ctrl->erst.entries,
+                        ERST_NUM_SEGS * sizeof(struct xhci_erst_entry));
 
        deq = (unsigned long)ctrl->event_ring->dequeue;
 
@@ -496,10 +578,13 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,
        /* this is the event ring segment table pointer */
        val_64 = xhci_readq(&ctrl->ir_set->erst_base);
        val_64 &= ERST_PTR_MASK;
-       val_64 |= ((u32)(ctrl->erst.entries) & ~ERST_PTR_MASK);
+       val_64 |= ((uintptr_t)(ctrl->erst.entries) & ~ERST_PTR_MASK);
 
        xhci_writeq(&ctrl->ir_set->erst_base, val_64);
 
+       /* set up the scratchpad buffer array and scratchpad buffers */
+       xhci_scratchpad_alloc(ctrl);
+
        /* initializing the virtual devices to NULL */
        for (i = 0; i < MAX_HC_SLOTS; ++i)
                ctrl->devs[i] = NULL;
@@ -627,17 +712,23 @@ void xhci_slot_copy(struct xhci_ctrl *ctrl, struct xhci_container_ctx *in_ctx,
  * @param udev pointer to the Device Data Structure
  * @return returns negative value on failure else 0 on success
  */
-void xhci_setup_addressable_virt_dev(struct usb_device *udev)
+void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
+                                    struct usb_device *udev, int hop_portnr)
 {
-       struct usb_device *hop = udev;
        struct xhci_virt_device *virt_dev;
        struct xhci_ep_ctx *ep0_ctx;
        struct xhci_slot_ctx *slot_ctx;
        u32 port_num = 0;
        u64 trb_64 = 0;
-       struct xhci_ctrl *ctrl = udev->controller;
+       int slot_id = udev->slot_id;
+       int speed = udev->speed;
+       int route = 0;
+#ifdef CONFIG_DM_USB
+       struct usb_device *dev = udev;
+       struct usb_hub_device *hub;
+#endif
 
-       virt_dev = ctrl->devs[udev->slot_id];
+       virt_dev = ctrl->devs[slot_id];
 
        BUG_ON(!virt_dev);
 
@@ -646,9 +737,34 @@ void xhci_setup_addressable_virt_dev(struct usb_device *udev)
        slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->in_ctx);
 
        /* Only the control endpoint is valid - one endpoint context */
-       slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1) | 0);
+       slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
+
+#ifdef CONFIG_DM_USB
+       /* Calculate the route string for this device */
+       port_num = dev->portnr;
+       while (!usb_hub_is_root_hub(dev->dev)) {
+               hub = dev_get_uclass_priv(dev->dev);
+               /*
+                * Each hub in the topology is expected to have no more than
+                * 15 ports in order for the route string of a device to be
+                * unique. SuperSpeed hubs are restricted to only having 15
+                * ports, but FS/LS/HS hubs are not. The xHCI specification
+                * says that if the port number the device is greater than 15,
+                * that portion of the route string shall be set to 15.
+                */
+               if (port_num > 15)
+                       port_num = 15;
+               route |= port_num << (hub->hub_depth * 4);
+               dev = dev_get_parent_priv(dev->dev);
+               port_num = dev->portnr;
+               dev = dev_get_parent_priv(dev->dev->parent);
+       }
 
-       switch (udev->speed) {
+       debug("route string %x\n", route);
+#endif
+       slot_ctx->dev_info |= route;
+
+       switch (speed) {
        case USB_SPEED_SUPER:
                slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
                break;
@@ -666,11 +782,31 @@ void xhci_setup_addressable_virt_dev(struct usb_device *udev)
                BUG();
        }
 
-       /* Extract the root hub port number */
-       if (hop->parent)
-               while (hop->parent->parent)
-                       hop = hop->parent;
-       port_num = hop->portnr;
+#ifdef CONFIG_DM_USB
+       /* Set up TT fields to support FS/LS devices */
+       if (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) {
+               struct udevice *parent = udev->dev;
+
+               dev = udev;
+               do {
+                       port_num = dev->portnr;
+                       dev = dev_get_parent_priv(parent);
+                       if (usb_hub_is_root_hub(dev->dev))
+                               break;
+                       parent = dev->dev->parent;
+               } while (dev->speed != USB_SPEED_HIGH);
+
+               if (!usb_hub_is_root_hub(dev->dev)) {
+                       hub = dev_get_uclass_priv(dev->dev);
+                       if (hub->tt.multi)
+                               slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
+                       slot_ctx->tt_info |= cpu_to_le32(TT_PORT(port_num));
+                       slot_ctx->tt_info |= cpu_to_le32(TT_SLOT(dev->slot_id));
+               }
+       }
+#endif
+
+       port_num = hop_portnr;
        debug("port_num = %d\n", port_num);
 
        slot_ctx->dev_info2 |=
@@ -680,9 +816,9 @@ void xhci_setup_addressable_virt_dev(struct usb_device *udev)
        /* Step 4 - ring already allocated */
        /* Step 5 */
        ep0_ctx->ep_info2 = cpu_to_le32(CTRL_EP << EP_TYPE_SHIFT);
-       debug("SPEED = %d\n", udev->speed);
+       debug("SPEED = %d\n", speed);
 
-       switch (udev->speed) {
+       switch (speed) {
        case USB_SPEED_SUPER:
                ep0_ctx->ep_info2 |= cpu_to_le32(((512 & MAX_PACKET_MASK) <<
                                        MAX_PACKET_SHIFT));
@@ -713,8 +849,14 @@ void xhci_setup_addressable_virt_dev(struct usb_device *udev)
        trb_64 = (uintptr_t)virt_dev->eps[0].ring->first_seg->trbs;
        ep0_ctx->deq = cpu_to_le64(trb_64 | virt_dev->eps[0].ring->cycle_state);
 
+       /*
+        * xHCI spec 6.2.3:
+        * software shall set 'Average TRB Length' to 8 for control endpoints.
+        */
+       ep0_ctx->tx_info = cpu_to_le32(EP_AVG_TRB_LENGTH(8));
+
        /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
 
-       xhci_flush_cache((uint32_t)ep0_ctx, sizeof(struct xhci_ep_ctx));
-       xhci_flush_cache((uint32_t)slot_ctx, sizeof(struct xhci_slot_ctx));
+       xhci_flush_cache((uintptr_t)ep0_ctx, sizeof(struct xhci_ep_ctx));
+       xhci_flush_cache((uintptr_t)slot_ctx, sizeof(struct xhci_slot_ctx));
 }