From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
Date: Sat, 1 Oct 2016 22:54:48 +0200
Subject: [PATCH] usb: xhci: add support for performing fake doorbell

Broadcom's Northstar XHCI controllers seem to need a special start
procedure to work correctly. There isn't any official documentation of
this, the problem is that controller doesn't detect any connected
devices with default setup. Moreover connecting USB device to controller
that doesn't run properly can cause SoC's watchdog issues.

A workaround that was successfully tested on multiple devices is to
perform a fake doorbell. This patch adds code for doing this and enables
it on BCM4708 family.
---
 drivers/usb/host/xhci-plat.c |  6 +++++
 drivers/usb/host/xhci.c      | 63 +++++++++++++++++++++++++++++++++++++++++---
 drivers/usb/host/xhci.h      |  1 +
 3 files changed, 67 insertions(+), 3 deletions(-)

--- a/drivers/usb/host/xhci-plat.c
+++ b/drivers/usb/host/xhci-plat.c
@@ -77,6 +77,8 @@ static int xhci_priv_resume_quirk(struct
 static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
 {
 	struct xhci_plat_priv *priv = xhci_to_priv(xhci);
+	struct platform_device*pdev = to_platform_device(dev);
+	struct device_node *node = pdev->dev.of_node;
 
 	/*
 	 * As of now platform drivers don't provide MSI support so we ensure
@@ -84,6 +86,9 @@ static void xhci_plat_quirks(struct devi
 	 * dev struct in order to setup MSI
 	 */
 	xhci->quirks |= XHCI_PLAT | priv->quirks;
+
+	if (node && of_machine_is_compatible("brcm,bcm4708"))
+		xhci->quirks |= XHCI_FAKE_DOORBELL;
 }
 
 /* called during probe() after chip reset completes */
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -158,6 +158,49 @@ int xhci_start(struct xhci_hcd *xhci)
 	return ret;
 }
 
+/**
+ * xhci_fake_doorbell - Perform a fake doorbell on a specified slot
+ *
+ * Some controllers require a fake doorbell to start correctly. Without that
+ * they simply don't detect any devices.
+ */
+static int xhci_fake_doorbell(struct xhci_hcd *xhci, int slot_id)
+{
+	u32 temp;
+
+	/* Alloc a virt device for that slot */
+	if (!xhci_alloc_virt_device(xhci, slot_id, NULL, GFP_NOIO)) {
+		xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
+		return -ENOMEM;
+	}
+
+	/* Ring fake doorbell for slot_id ep 0 */
+	xhci_ring_ep_doorbell(xhci, slot_id, 0, 0);
+	usleep_range(1000, 1500);
+
+	/* Read the status to check if HSE is set or not */
+	temp = readl(&xhci->op_regs->status);
+
+	/* Clear HSE if set */
+	if (temp & STS_FATAL) {
+		xhci_dbg(xhci, "HSE problem detected, status: 0x%08x\n", temp);
+		temp &= ~0x1fff;
+		temp |= STS_FATAL;
+		writel(temp, &xhci->op_regs->status);
+		usleep_range(1000, 1500);
+		readl(&xhci->op_regs->status);
+	}
+
+	/* Free virt device */
+	xhci_free_virt_device(xhci, slot_id);
+
+	/* We're done if controller is already running */
+	if (readl(&xhci->op_regs->command) & CMD_RUN)
+		return 0;
+
+	return xhci_start(xhci);
+}
+
 /*
  * Reset a halted HC.
  *
@@ -634,6 +677,16 @@ static int xhci_run_finished(struct xhci
 		spin_unlock_irqrestore(&xhci->lock, flags);
 		return -ENODEV;
 	}
+
+	if (xhci->quirks & XHCI_FAKE_DOORBELL) {
+		int err = xhci_fake_doorbell(xhci, 1);
+		if (err) {
+			xhci_halt(xhci);
+			spin_unlock_irqrestore(&xhci->lock, flags);
+			return err;
+		}
+	}
+
 	xhci->shared_hcd->state = HC_STATE_RUNNING;
 	xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
 
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -1908,6 +1908,7 @@ struct xhci_hcd {
 #define XHCI_RESET_TO_DEFAULT	BIT_ULL(44)
 #define XHCI_ZHAOXIN_TRB_FETCH	BIT_ULL(45)
 #define XHCI_ZHAOXIN_HOST	BIT_ULL(46)
+#define XHCI_FAKE_DOORBELL     BIT_ULL(47)
 
 	unsigned int		num_active_eps;
 	unsigned int		limit_active_eps;
