From ee7c3ab6b5a4d284a04f110792508a7f8decd7f7 Mon Sep 17 00:00:00 2001
From: William Wu <william.wu@rock-chips.com>
Date: Tue, 6 Dec 2022 14:45:54 +0800
Subject: [PATCH] usb: dwc2: fix waiting time for host only mode

The current code uses 50ms sleep to wait for host only
mode, the delay time is not enough for some Rockchip
platforms (e.g RK3036G EVB1).

Test on RK3036G EVB1, the dwc2 host only controller reg
GOTGCTL.ConIDSts = 1'b1 (device mode) if only wait for
50ms. And the host fails to detect usb2 device with the
following error log:

usb usb2-port1: connect-debounce failed

This patch checks the GOTGCTL.ConIDSts for host only
mode and increases the maximum waiting time to 200ms.

Signed-off-by: William Wu <william.wu@rock-chips.com>
Change-Id: Ie28299934aba09907ea08f5fd3b34bf2fb35822e
---
 drivers/usb/dwc2/core.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/dwc2/core.c b/drivers/usb/dwc2/core.c
index 15911ac7582b4..cbd5f1142f35e 100644
--- a/drivers/usb/dwc2/core.c
+++ b/drivers/usb/dwc2/core.c
@@ -656,14 +656,24 @@ static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
  */
 void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
 {
+	u32 count = 0;
+
 	switch (hsotg->dr_mode) {
 	case USB_DR_MODE_HOST:
 		/*
 		 * NOTE: This is required for some rockchip soc based
 		 * platforms on their host-only dwc2.
 		 */
-		if (!dwc2_hw_is_otg(hsotg))
-			msleep(50);
+		if (!dwc2_hw_is_otg(hsotg)) {
+			while (dwc2_readl(hsotg, GOTGCTL) & GOTGCTL_CONID_B) {
+				msleep(20);
+				if (++count > 10)
+					break;
+			}
+			if (count > 10)
+				dev_err(hsotg->dev,
+					"Waiting for Host Mode timed out");
+		}
 
 		break;
 	case USB_DR_MODE_PERIPHERAL:
