]> git.ipfire.org Git - people/ms/u-boot.git/commitdiff
usb: xhci: Initialize scratchpad buffer array and scratchpad buffers
authorBin Meng <bmeng.cn@gmail.com>
Wed, 19 Jul 2017 13:49:55 +0000 (21:49 +0800)
committerMarek Vasut <marex@denx.de>
Fri, 28 Jul 2017 21:34:16 +0000 (23:34 +0200)
The scratchpad buffer array is used to define the locations of
statically allocated memory pages that are available for the
private use of the xHC. The xHCI spec explicitly mentions that
system software shall allocate the scratchpad buffers before
placing the xHC in to Run mode (Run/Stop (R/S) = ‘1’), however
U-Boot is missing this part.

This causes xHC on Intel platform does not respond the very first
'enable slot' command that is given to xHC and the 'enable slot'
command completion event TRB is never generated and xHC seems to
hang forever.

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Stefan Roese <sr@denx.de>
Tested-by: Stefan Roese <sr@denx.de>
drivers/usb/host/xhci-mem.c
drivers/usb/host/xhci.h

index 62db51d01851c1eb1909a79de4271cf0e0ff7497..12e277a26d988b3554cb665452e78039cb58cfac 100644 (file)
@@ -95,6 +95,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
  *
@@ -155,6 +174,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);
@@ -319,6 +339,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
  *
@@ -499,6 +583,9 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,
 
        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;
index 431afd864dbd98f7c1f5926b43f6a0e600733375..32dd611efacf195c884c2dde9afb2b412cb74cae 100644 (file)
@@ -111,9 +111,10 @@ struct xhci_hccr {
 #define HCS_IST(p)             (((p) >> 0) & 0xf)
 /* bits 4:7, max number of Event Ring segments */
 #define HCS_ERST_MAX(p)                (((p) >> 4) & 0xf)
+/* bits 21:25 Hi 5 bits of Scratchpad buffers SW must allocate for the HW */
 /* bit 26 Scratchpad restore - for save/restore HW state - not used yet */
-/* bits 27:31 number of Scratchpad buffers SW must allocate for the HW */
-#define HCS_MAX_SCRATCHPAD(p)   (((p) >> 27) & 0x1f)
+/* bits 27:31 Lo 5 bits of Scratchpad buffers SW must allocate for the HW */
+#define HCS_MAX_SCRATCHPAD(p)  ((((p) >> 16) & 0x3e0) | (((p) >> 27) & 0x1f))
 
 /* HCSPARAMS3 - hcs_params3 - bitmasks */
 /* bits 0:7, Max U1 to U0 latency for the roothub ports */
@@ -1037,6 +1038,10 @@ struct xhci_erst {
        unsigned int            erst_size;
 };
 
+struct xhci_scratchpad {
+       u64 *sp_array;
+};
+
 /*
  * Each segment table entry is 4*32bits long.  1K seems like an ok size:
  * (1K bytes * 8bytes/bit) / (4*32 bits) = 64 segment entries in the table,
@@ -1224,6 +1229,7 @@ struct xhci_ctrl {
        struct xhci_intr_reg *ir_set;
        struct xhci_erst erst;
        struct xhci_erst_entry entry[ERST_NUM_SEGS];
+       struct xhci_scratchpad *scratchpad;
        struct xhci_virt_device *devs[MAX_HC_SLOTS];
        int rootdev;
 };