xref: /rk3399_rockchip-uboot/arch/arm/mach-rockchip/boot_mode.c (revision e17ddcea32b2fa7b82fb079f37195855a55e39a2)
1 /*
2  * (C) Copyright 2016 Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <adc.h>
9 #include <asm/io.h>
10 #include <asm/arch/boot_mode.h>
11 #include <cli.h>
12 #include <dm.h>
13 #include <fdtdec.h>
14 #include <boot_rkimg.h>
15 #include <linux/usb/phy-rockchip-inno-usb2.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
19 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG == 0)
20 
21 int setup_boot_mode(void)
22 {
23 	return 0;
24 }
25 
26 #else
27 
28 void set_back_to_bootrom_dnl_flag(void)
29 {
30 	writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG);
31 }
32 
33 /*
34  * detect download key status by adc, most rockchip
35  * based boards use adc sample the download key status,
36  * but there are also some use gpio. So it's better to
37  * make this a weak function that can be override by
38  * some special boards.
39  */
40 #define KEY_DOWN_MIN_VAL	0
41 #define KEY_DOWN_MAX_VAL	30
42 
43 __weak int rockchip_dnl_key_pressed(void)
44 {
45 	const void *blob = gd->fdt_blob;
46 	unsigned int val;
47 	int channel = 1;
48 	int node;
49 	u32 chns[2];
50 
51 	node = fdt_node_offset_by_compatible(blob, 0, "adc-keys");
52 	if (node >= 0) {
53 	       if (!fdtdec_get_int_array(blob, node, "io-channels", chns, 2))
54 		       channel = chns[1];
55 	}
56 
57 	if (adc_channel_single_shot("saradc", channel, &val)) {
58 		printf("%s adc_channel_single_shot fail!\n", __func__);
59 		return false;
60 	}
61 
62 	if ((val >= KEY_DOWN_MIN_VAL) && (val <= KEY_DOWN_MAX_VAL))
63 		return true;
64 	else
65 		return false;
66 }
67 
68 void devtype_num_envset(void)
69 {
70 	static int done = 0;
71 
72 	if (done)
73 		return;
74 
75 	const char *devtype_num_set =
76 	"if mmc dev 0; then setenv devtype mmc; setenv devnum 0;"
77 	"else if rknand dev 0; then setenv devtype rknand; setenv devnum 0; fi;"
78 	"fi;";
79 
80 	run_command_list(devtype_num_set, -1, 0);
81 	done = 1;
82 }
83 
84 void rockchip_dnl_mode_check(void)
85 {
86 	if (rockchip_dnl_key_pressed() && rockchip_u2phy_vbus_detect()) {
87 		printf("download key pressed, entering download mode...\n");
88 		/* If failed, we fall back to bootrom download mode */
89 		run_command_list("rockusb 0 ${devtype} ${devnum}", -1, 0);
90 		set_back_to_bootrom_dnl_flag();
91 		do_reset(NULL, 0, 0, NULL);
92 	}
93 }
94 
95 int setup_boot_mode(void)
96 {
97 	int boot_mode = BOOT_MODE_NORMAL;
98 	char env_preboot[256] = {0};
99 
100 	devtype_num_envset();
101 	rockchip_dnl_mode_check();
102 #ifdef CONFIG_RKIMG_BOOTLOADER
103 	boot_mode = rockchip_get_boot_mode();
104 #endif
105 	switch (boot_mode) {
106 	case BOOT_MODE_BOOTLOADER:
107 		printf("enter fastboot!\n");
108 #if defined(CONFIG_FASTBOOT_FLASH_MMC_DEV)
109 		snprintf(env_preboot, 256,
110 				"setenv preboot; mmc dev %x; fastboot usb 0; ",
111 				CONFIG_FASTBOOT_FLASH_MMC_DEV);
112 #elif defined(CONFIG_FASTBOOT_FLASH_NAND_DEV)
113 		snprintf(env_preboot, 256,
114 				"setenv preboot; fastboot usb 0; ");
115 #endif
116 		env_set("preboot", env_preboot);
117 		break;
118 	case BOOT_MODE_UMS:
119 		printf("enter UMS!\n");
120 		env_set("preboot", "setenv preboot; ums mmc 0");
121 		break;
122 	case BOOT_MODE_LOADER:
123 		printf("enter Rockusb!\n");
124 		env_set("preboot", "setenv preboot; rockusb 0 ${devtype} ${devnum}");
125 		break;
126 	case BOOT_MODE_CHARGING:
127 		printf("enter charging!\n");
128 		env_set("preboot", "setenv preboot; charge");
129 		break;
130 	case BOOT_MODE_RECOVERY:
131 		printf("enter Recovery mode!\n");
132 		env_set("reboot_mode", "recovery");
133 		break;
134 	}
135 
136 	return 0;
137 }
138 
139 #endif
140