From b5a62178cd25c598612036858df5934c4140d242 Mon Sep 17 00:00:00 2001
From: Jonathan Bell <jonathan@raspberrypi.com>
Date: Fri, 16 Dec 2022 15:18:21 +0000
Subject: [PATCH] xhci: constrain XHCI_VLI_HUB_TT_QUIRK to old firmware
 versions

VLI have a firmware update for the VL805 which resolves the incorrect
frame time calculation in the hub's TT. Limit applying the quirk to
known-bad firmwares.

Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
---
 drivers/usb/host/xhci-pci.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

--- a/drivers/usb/host/xhci-pci.c
+++ b/drivers/usb/host/xhci-pci.c
@@ -27,6 +27,8 @@
 #define SPARSE_DISABLE_BIT	17
 #define SPARSE_CNTL_ENABLE	0xC12C
 
+#define VL805_FW_VER_0138C0	0x0138C0
+
 /* Device for a quirk */
 #define PCI_VENDOR_ID_FRESCO_LOGIC	0x1b73
 #define PCI_DEVICE_ID_FRESCO_LOGIC_PDK	0x1000
@@ -104,6 +106,16 @@ static int xhci_pci_reinit(struct xhci_h
 	return 0;
 }
 
+static u32 xhci_vl805_get_fw_version(struct pci_dev *dev)
+{
+	int ret;
+	u32 ver;
+
+	ret = pci_read_config_dword(dev, 0x50, &ver);
+	/* Default to a fw version of 0 instead of ~0 */
+	return ret ? 0 : ver;
+}
+
 static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci)
 {
 	struct pci_dev                  *pdev = to_pci_dev(dev);
@@ -298,7 +310,8 @@ static void xhci_pci_quirks(struct devic
 		xhci->quirks |= XHCI_AVOID_DQ_ON_LINK;
 		xhci->quirks |= XHCI_VLI_TRB_CACHE_BUG;
 		xhci->quirks |= XHCI_VLI_SS_BULK_OUT_BUG;
-		xhci->quirks |= XHCI_VLI_HUB_TT_QUIRK;
+		if (xhci_vl805_get_fw_version(pdev) < VL805_FW_VER_0138C0)
+			xhci->quirks |= XHCI_VLI_HUB_TT_QUIRK;
 	}
 
 	if (pdev->vendor == PCI_VENDOR_ID_ASMEDIA &&
