From: Hans de Goede <hdegoede@redhat.com>
Date: Fri, 27 Mar 2015 20:46:00 +0000 (+0100)
Subject: sunxi: usbc: Wait for vbus to fall after disabling it
X-Git-Tag: v2025.01-rc5-pxa1908~13392^2~15
X-Git-Url: http://git.dujemihanovic.xyz/img/html/%7B%7B%20%28.OutputFormats.Get?a=commitdiff_plain;h=a0e2b1b8659b041ce368ee0644a410d3ce7c1f8c;p=u-boot.git

sunxi: usbc: Wait for vbus to fall after disabling it

When u-boot boots the board may be powering vbus, we turn off vbus in
sunxi_usbc_request_resources, if we are too quick with reading vusb-detect
after this we may see a residual charge and assume we've an external vusb
connected even though we do not. So when we see an external vusb wait a bit
and try again.

Without this when dealing with a pmic controller vbus and doing "reset" on
the u-boot console the musb host will only init once every other boot, because
the other boot it thinks an external vbus is present, this commit fixes this.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Acked-by: Ian Campbell <ijc@hellion.org.uk>
---

diff --git a/arch/arm/cpu/armv7/sunxi/usbc.c b/arch/arm/cpu/armv7/sunxi/usbc.c
index 6285fa5ecb..a0e9604cfa 100644
--- a/arch/arm/cpu/armv7/sunxi/usbc.c
+++ b/arch/arm/cpu/armv7/sunxi/usbc.c
@@ -286,12 +286,23 @@ void sunxi_usbc_vbus_disable(int index)
 int sunxi_usbc_vbus_detect(int index)
 {
 	struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index];
-	int err;
+	int err, retries = 3;
 
 	if (sunxi_usbc->gpio_vbus_det == -1) {
 		eprintf("Error: invalid vbus detection pin\n");
 		return -1;
 	}
 
-	return gpio_get_value(sunxi_usbc->gpio_vbus_det);
+	err = gpio_get_value(sunxi_usbc->gpio_vbus_det);
+	/*
+	 * Vbus may have been provided by the board and just been turned of
+	 * some milliseconds ago on reset, what we're measuring then is a
+	 * residual charge on Vbus, sleep a bit and try again.
+	 */
+	while (err > 0 && retries--) {
+		mdelay(100);
+		err = gpio_get_value(sunxi_usbc->gpio_vbus_det);
+	}
+
+	return err;
 }