From 5ede1fabf35a0a4f041d619e69d07c4fa60ca155 Mon Sep 17 00:00:00 2001
From: Paolo Sabatino <paolo.sabatino@gmail.com>
Date: Sat, 19 Mar 2022 11:50:17 +0000
Subject: [PATCH] rk3288: autoenable UMS mode if OTG port is connected to PC

---
 arch/arm/include/asm/arch-rockchip/gpio.h | 22 ++++++++
 arch/arm/mach-rockchip/board.c            | 65 +++++++++++++++++++++++
 cmd/usb_mass_storage.c                    |  2 +-
 common/autoboot.c                         | 17 ++++++
 common/board_r.c                          |  1 +
 include/init.h                            |  1 +
 6 files changed, 107 insertions(+), 1 deletion(-)

diff --git a/arch/arm/include/asm/arch-rockchip/gpio.h b/arch/arm/include/asm/arch-rockchip/gpio.h
index 1aaec5fa..135688d3 100644
--- a/arch/arm/include/asm/arch-rockchip/gpio.h
+++ b/arch/arm/include/asm/arch-rockchip/gpio.h
@@ -24,6 +24,28 @@ struct rockchip_gpio_regs {
 };
 check_member(rockchip_gpio_regs, ls_sync, 0x60);

+/*
+ * RK3288 IO memory map:
+ *
+ */
+#define RKIO_GPIO0_PHYS                 0xFF750000
+#define RKIO_GRF_PHYS                   0xFF770000
+#define RKIO_GPIO1_PHYS                 0xFF780000
+#define RKIO_GPIO2_PHYS                 0xFF790000
+#define RKIO_GPIO3_PHYS                 0xFF7A0000
+#define RKIO_GPIO4_PHYS                 0xFF7B0000
+#define RKIO_GPIO5_PHYS                 0xFF7C0000
+#define RKIO_GPIO6_PHYS                 0xFF7D0000
+
+/* gpio power down/up control */
+#define GRF_GPIO2A_P		0x150
+#define GRF_GPIO6A_P		0x190
+
+/* gpio input/output control */
+#define GPIO_SWPORT_DR		0x00
+#define GPIO_SWPORT_DDR		0x04
+#define GPIO_EXT_PORT		0x50
+
 enum gpio_pu_pd {
 	GPIO_PULL_NORMAL = 0,
 	GPIO_PULL_UP,
diff --git a/arch/arm/mach-rockchip/board.c b/arch/arm/mach-rockchip/board.c
index ba4da72b..fc53f2e8 100644
--- a/arch/arm/mach-rockchip/board.c
+++ b/arch/arm/mach-rockchip/board.c
@@ -21,6 +21,20 @@

 DECLARE_GLOBAL_DATA_PTR;

+enum project_id {
+	TinkerBoardS = 0,
+	TinkerBoard  = 7,
+};
+
+enum pcb_id {
+	SR,
+	ER,
+	PR,
+};
+
+extern bool force_ums;
+
+
 __weak int rk_board_late_init(void)
 {
 	return 0;
@@ -33,6 +47,57 @@ int board_late_init(void)
 	return rk_board_late_init();
 }

+int check_force_enter_ums_mode(void)
+{
+	int tmp;
+	enum pcb_id pcbid;
+	enum project_id projectid;
+
+	// GPIO2_A1/GPIO2_A2/GPIO2_A3 pull up enable
+	// please check TRM V1.2 part1 page 152
+	tmp = readl(RKIO_GRF_PHYS + GRF_GPIO2A_P);
+	writel((tmp&~(0x03F<<2)) | 0x3F<<(16 + 2) | 0x15<<2, RKIO_GRF_PHYS + GRF_GPIO2A_P);
+
+	// GPIO2_A1/GPIO2_A2/GPIO2_A3/GPIO2_B0/GPIO2_B1/GPIO2_B2 set to input
+	tmp = readl(RKIO_GPIO2_PHYS + GPIO_SWPORT_DDR);
+	writel(tmp & ~(0x70E), RKIO_GPIO2_PHYS + GPIO_SWPORT_DDR);
+
+	// GPIO6_A5 pull up/down disable
+	tmp = readl(RKIO_GRF_PHYS + GRF_GPIO6A_P);
+	writel((tmp&~(0x03<<10)) | 0x03<<(16 + 10), RKIO_GRF_PHYS + GRF_GPIO6A_P);
+
+	// GPIO6_A5 set to input
+	tmp = readl(RKIO_GPIO6_PHYS + GPIO_SWPORT_DDR);
+	writel(tmp & ~(0x20), RKIO_GPIO6_PHYS + GPIO_SWPORT_DDR);
+
+	mdelay(10);
+
+	// read GPIO2_A1/GPIO2_A2/GPIO2_A3 value
+	projectid = (readl(RKIO_GPIO2_PHYS + GPIO_EXT_PORT) & 0x0E) >>1;
+
+	// read GPIO2_B0/GPIO2_B1/GPIO2_B2 value
+	pcbid = (readl(RKIO_GPIO2_PHYS + GPIO_EXT_PORT) & 0x700) >> 8;
+
+	// only Tinker Board S and the PR stage PCB has this function
+	if(projectid!=TinkerBoard && pcbid >= ER){
+		printf("PC event = 0x%x\n", readl(RKIO_GPIO6_PHYS + GPIO_EXT_PORT)&0x20);
+		if((readl(RKIO_GPIO6_PHYS + GPIO_EXT_PORT)&0x20)==0x20) {
+			// SDP detected, enable EMMC and unlock usb current limit
+			printf("usb connected to SDP, force enter ums mode\n");
+			force_ums = true;
+			// unlock usb current limit and re-enable EMMC
+			// set GPIO6_A6, GPIO6_A7 to high
+			tmp = readl(RKIO_GPIO6_PHYS + GPIO_SWPORT_DR);
+			writel(tmp | 0xc0, RKIO_GPIO6_PHYS + GPIO_SWPORT_DR);
+			tmp = readl(RKIO_GPIO6_PHYS + GPIO_SWPORT_DDR);
+			writel(tmp | 0xc0, RKIO_GPIO6_PHYS + GPIO_SWPORT_DDR);
+			mdelay(10);
+		}
+	}
+	return 0;
+}
+
+
 int board_init(void)
 {
 	int ret;
diff --git a/cmd/usb_mass_storage.c b/cmd/usb_mass_storage.c
index cf2f5599..ee8a2802 100644
--- a/cmd/usb_mass_storage.c
+++ b/cmd/usb_mass_storage.c
@@ -136,7 +136,7 @@ cleanup:
 	return ret;
 }

-static int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag,
+int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag,
 			       int argc, char *const argv[])
 {
 	const char *usb_controller;
diff --git a/common/autoboot.c b/common/autoboot.c
index e628baff..ea282664 100644
--- a/common/autoboot.c
+++ b/common/autoboot.c
@@ -37,6 +37,8 @@ DECLARE_GLOBAL_DATA_PTR;
 static int stored_bootdelay;
 static int menukey;

+bool force_ums = false;
+
 #ifdef CONFIG_AUTOBOOT_ENCRYPTION
 #define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
 #else
@@ -49,6 +51,8 @@ static int menukey;
 #define AUTOBOOT_MENUKEY 0
 #endif

+extern int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
+
 /*
  * Use a "constant-length" time compare function for this
  * hash compare:
@@ -363,6 +367,19 @@ void autoboot_command(const char *s)
 {
 	debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");

+	if (force_ums) {
+		// force to enter ums mode
+		char *local_args[4];
+		char str1[]="ums", str2[]="1", str3[]="mmc", str4[]="0";
+
+		local_args[0]=str1;
+		local_args[1]=str2;
+		local_args[2]=str3;
+		local_args[3]=str4;
+		do_usb_mass_storage(NULL, 0, 4, local_args);
+		return;
+	}
+
 	if (s && (stored_bootdelay == -2 ||
 		 (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
 		bool lock;
diff --git a/common/board_r.c b/common/board_r.c
index 29dd7d26..5b952d00 100644
--- a/common/board_r.c
+++ b/common/board_r.c
@@ -797,6 +797,7 @@ static init_fnc_t init_sequence_r[] = {
 #ifdef CONFIG_MMC
 	initr_mmc,
 #endif
+	check_force_enter_ums_mode,
 #ifdef CONFIG_XEN
 	initr_xen,
 #endif
diff --git a/include/init.h b/include/init.h
index 0f48ccb5..bae1cb88 100644
--- a/include/init.h
+++ b/include/init.h
@@ -261,6 +261,7 @@ int board_early_init_f(void);
 /* manipulate the U-Boot fdt before its relocation */
 int board_fix_fdt(void *rw_fdt_blob);
 int board_late_init(void);
+int check_force_enter_ums_mode (void);
 int board_postclk_init(void); /* after clocks/timebase, before env/serial */
 int board_early_init_r(void);

--
2.30.2
