xref: /rk3399_rockchip-uboot/arch/arm/mach-rockchip/boot_mode.c (revision 1636e7c2d44abbfb7db4fa675149e822d0b2cc99)
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 	int ret = 0;
72 
73 	if (done)
74 		return;
75 
76 	const char *devtype_num_set = "run rkimg_bootdev";
77 
78 	ret = run_command_list(devtype_num_set, -1, 0);
79 	if (ret) {
80 		/* Set default dev type/num if command not valid */
81 		env_set("devtype", "mmc");
82 		env_set("devnum", "0");
83 	}
84 
85 	done = 1;
86 }
87 
88 void rockchip_dnl_mode_check(void)
89 {
90 	if (rockchip_dnl_key_pressed()) {
91 		if (rockchip_u2phy_vbus_detect()) {
92 			printf("download key pressed, entering download mode...\n");
93 			/* If failed, we fall back to bootrom download mode */
94 			run_command_list("rockusb 0 ${devtype} ${devnum}", -1, 0);
95 			set_back_to_bootrom_dnl_flag();
96 			do_reset(NULL, 0, 0, NULL);
97 		} else {
98 			printf("recovery key pressed, entering recovery mode!\n");
99 			env_set("reboot_mode", "recovery");
100 		}
101 	}
102 }
103 
104 int setup_boot_mode(void)
105 {
106 	int boot_mode = BOOT_MODE_NORMAL;
107 	char env_preboot[256] = {0};
108 
109 	devtype_num_envset();
110 	rockchip_dnl_mode_check();
111 #ifdef CONFIG_RKIMG_BOOTLOADER
112 	boot_mode = rockchip_get_boot_mode();
113 #endif
114 	switch (boot_mode) {
115 	case BOOT_MODE_BOOTLOADER:
116 		printf("enter fastboot!\n");
117 #if defined(CONFIG_FASTBOOT_FLASH_MMC_DEV)
118 		snprintf(env_preboot, 256,
119 				"setenv preboot; mmc dev %x; fastboot usb 0; ",
120 				CONFIG_FASTBOOT_FLASH_MMC_DEV);
121 #elif defined(CONFIG_FASTBOOT_FLASH_NAND_DEV)
122 		snprintf(env_preboot, 256,
123 				"setenv preboot; fastboot usb 0; ");
124 #endif
125 		env_set("preboot", env_preboot);
126 		break;
127 	case BOOT_MODE_UMS:
128 		printf("enter UMS!\n");
129 		env_set("preboot", "setenv preboot; ums mmc 0");
130 		break;
131 	case BOOT_MODE_LOADER:
132 		printf("enter Rockusb!\n");
133 		env_set("preboot", "setenv preboot; rockusb 0 ${devtype} ${devnum}");
134 		break;
135 	case BOOT_MODE_CHARGING:
136 		printf("enter charging!\n");
137 		env_set("preboot", "setenv preboot; charge");
138 		break;
139 	case BOOT_MODE_RECOVERY:
140 		printf("enter Recovery mode!\n");
141 		env_set("reboot_mode", "recovery");
142 		break;
143 	}
144 
145 	return 0;
146 }
147 
148 #endif
149