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