From d58a0133890ea4610f8fa7ff5d3a6960d1247d4c Mon Sep 17 00:00:00 2001
From: Mattijs Korpershoek <mkorpershoek@baylibre.com>
Date: Thu, 7 Jul 2022 16:40:21 +0200
Subject: [PATCH] HACK: board: vim3: reset usb controller upon system reset

On the VIM3L board, with some USB cables/hosts, there is a long (5s)
delay before between "fastboot reboot" and the host detecting a USB
reset.

This breaks tools relying on "fastboot reboot fastboot" which assume
that 1s after the command send, the board should disconnect on usb.

To reproduce, enable fastboot in U-Boot console:
=> fastboot usb 0

Then, on the host, run:
  # echo "running fastboot reboot bootloader" > /dev/kmsg && fastboot reboot bootloader
  Rebooting into bootloader                          OKAY [  0.003s]
  Finished. Total time: 3.033s

  [54074.251551] running fastboot reboot bootloader
  ... there is a delay of 5s before we detect a disconnection ...
  [54079.041238] usb 1-7.4: USB disconnect, device number 72
  [54079.239625] usb 1-7.4: new high-speed USB device number 73 using xhci_hcd
  [54079.359103] usb 1-7.4: New USB device found, idVendor=1b8e, idProduct=fada, bcdDevice= 2.27
  [54079.359110] usb 1-7.4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
  [54079.359112] usb 1-7.4: Product: USB download gadget
  [54079.359114] usb 1-7.4: Manufacturer: U-Boot
  [54079.359116] usb 1-7.4: SerialNumber: C8631470CC41

Note: this does not happen when we use the RST button on the board, nor in linux.

Resetting the usb controller before psci_reset fixes this.

Signed-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
---
 board/amlogic/vim3/vim3.c              | 25 +++++++++++++++++++++++++
 configs/khadas-vim3l_android_defconfig |  2 ++
 2 files changed, 27 insertions(+)

diff --git a/board/amlogic/vim3/vim3.c b/board/amlogic/vim3/vim3.c
index fcd60ab1e05..eb3052474b5 100644
--- a/board/amlogic/vim3/vim3.c
+++ b/board/amlogic/vim3/vim3.c
@@ -15,6 +15,8 @@
 #include <asm/arch/sm.h>
 #include <asm/global_data.h>
 #include <i2c.h>
+#include <linux/psci.h>
+#include <reset.h>
 #include "khadas-mcu.h"
 
 int mmc_get_env_dev(void)
@@ -188,3 +190,26 @@ int misc_init_r(void)
 
 	return 0;
 }
+
+void reset_misc(void)
+{
+	struct reset_ctl usb_reset;
+	struct udevice *dev;
+	ofnode node;
+	int ret;
+
+	node = ofnode_by_compatible(ofnode_null(), "amlogic,meson-g12a-usb-ctrl");
+	if (!ofnode_valid(node))
+		printf("vim3: cannot find amlogic,meson-g12a-usb-ctrl node\n");
+
+	ret = reset_get_by_index_nodev(node, 0, &usb_reset);
+	if (ret < 0)
+		printf("vim3: cannot find usb node reset property\n");
+
+	ret = reset_assert(&usb_reset);
+	if (ret < 0)
+		printf("vim3: cannot assert usb reset\n");
+
+	uclass_get_device_by_name(UCLASS_FIRMWARE, "psci", &dev);
+	invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
+}
diff --git a/configs/khadas-vim3l_android_defconfig b/configs/khadas-vim3l_android_defconfig
index 085919b9741..c16df76448a 100644
--- a/configs/khadas-vim3l_android_defconfig
+++ b/configs/khadas-vim3l_android_defconfig
@@ -1,6 +1,7 @@
 CONFIG_ARM=y
 CONFIG_SYS_BOARD="vim3"
 CONFIG_SYS_CONFIG_NAME="khadas-vim3l_android"
+CONFIG_ARM_SMCCC=y
 CONFIG_ARCH_MESON=y
 CONFIG_SYS_TEXT_BASE=0x01000000
 CONFIG_NR_DRAM_BANKS=1
@@ -11,6 +12,7 @@ CONFIG_MESON_G12A=y
 CONFIG_DEBUG_UART_BASE=0xff803000
 CONFIG_DEBUG_UART_CLOCK=24000000
 CONFIG_IDENT_STRING=" khadas-vim3l"
+# CONFIG_PSCI_RESET is not set
 CONFIG_DEBUG_UART=y
 CONFIG_REMAKE_ELF=y
 CONFIG_SYS_LOAD_ADDR=0x1000000
-- 
GitLab