xref: /rk3399_rockchip-uboot/arch/arm/mach-rockchip/boot_mode.c (revision 87e4c6020eff05133e40ab8b7b0e37e6a2be37e4)
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 <asm/arch/hotkey.h>
12 #include <asm/arch/param.h>
13 #include <cli.h>
14 #include <dm.h>
15 #include <fdtdec.h>
16 #include <boot_rkimg.h>
17 #include <stdlib.h>
18 #include <linux/usb/phy-rockchip-inno-usb2.h>
19 #include <key.h>
20 #ifdef CONFIG_DM_RAMDISK
21 #include <ramdisk.h>
22 #endif
23 #include <mmc.h>
24 #include <console.h>
25 
26 DECLARE_GLOBAL_DATA_PTR;
27 
28 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG == 0)
29 
30 int setup_boot_mode(void)
31 {
32 	return 0;
33 }
34 
35 #else
36 
37 void set_back_to_bootrom_dnl_flag(void)
38 {
39 	writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG);
40 }
41 
42 /*
43  * detect download key status by adc, most rockchip
44  * based boards use adc sample the download key status,
45  * but there are also some use gpio. So it's better to
46  * make this a weak function that can be override by
47  * some special boards.
48  */
49 #define KEY_DOWN_MIN_VAL	0
50 #define KEY_DOWN_MAX_VAL	30
51 
52 __weak int rockchip_dnl_key_pressed(void)
53 {
54 	int keyval = false;
55 
56 /*
57  * This is a generic interface to read key
58  */
59 #if defined(CONFIG_DM_KEY)
60 	keyval = key_read(KEY_VOLUMEUP);
61 
62 	return key_is_pressed(keyval);
63 
64 #elif defined(CONFIG_ADC)
65 	const void *blob = gd->fdt_blob;
66 	unsigned int val;
67 	int channel = 1;
68 	int node;
69 	int ret;
70 	u32 chns[2];
71 
72 	node = fdt_node_offset_by_compatible(blob, 0, "adc-keys");
73 	if (node >= 0) {
74 	       if (!fdtdec_get_int_array(blob, node, "io-channels", chns, 2))
75 		       channel = chns[1];
76 	}
77 
78 	ret = adc_channel_single_shot("saradc", channel, &val);
79 	if (ret) {
80 		printf("%s adc_channel_single_shot fail! ret=%d\n", __func__, ret);
81 		return false;
82 	}
83 
84 	if ((val >= KEY_DOWN_MIN_VAL) && (val <= KEY_DOWN_MAX_VAL))
85 		return true;
86 	else
87 		return false;
88 #endif
89 
90 	return keyval;
91 }
92 
93 void boot_devtype_init(void)
94 {
95 	const char *devtype_num_set = "run rkimg_bootdev";
96 	char *devtype = NULL, *devnum = NULL;
97 	static int done = 0;
98 	int atags_en = 0;
99 	int ret;
100 
101 	if (done)
102 		return;
103 
104 	ret = param_parse_bootdev(&devtype, &devnum);
105 	if (!ret) {
106 		atags_en = 1;
107 		env_set("devtype", devtype);
108 		env_set("devnum", devnum);
109 
110 #ifdef CONFIG_DM_MMC
111 		if (!strcmp("mmc", devtype))
112 			mmc_initialize(gd->bd);
113 #endif
114 		/*
115 		 * For example, the pre-loader do not have mtd device,
116 		 * and pass devtype is nand. Then U-Boot can not get
117 		 * dev_desc when use mtd driver to read firmware. So
118 		 * test the block dev is exist or not here.
119 		 *
120 		 * And the devtype & devnum maybe wrong sometimes, it
121 		 * is better to test first.
122 		 */
123 		if (blk_get_devnum_by_typename(devtype, atoi(devnum)))
124 			goto finish;
125 	}
126 
127 	/* If not find valid bootdev by atags, scan all possible */
128 #ifdef CONFIG_DM_MMC
129 	mmc_initialize(gd->bd);
130 #endif
131 	ret = run_command_list(devtype_num_set, -1, 0);
132 	if (ret) {
133 		/* Set default dev type/num if command not valid */
134 		devtype = "mmc";
135 		devnum = "0";
136 		env_set("devtype", devtype);
137 		env_set("devnum", devnum);
138 	}
139 
140 finish:
141 	done = 1;
142 	printf("Bootdev%s: %s %s\n", atags_en ? "(atags)" : "",
143 	       env_get("devtype"), env_get("devnum"));
144 }
145 
146 void rockchip_dnl_mode_check(void)
147 {
148 	/* recovery key or "ctrl+d" */
149 	if (rockchip_dnl_key_pressed() ||
150 	    is_hotkey(HK_ROCKUSB_DNL)) {
151 		printf("download key pressed... ");
152 		if (rockchip_u2phy_vbus_detect() > 0) {
153 			printf("entering download mode...\n");
154 			/* If failed, we fall back to bootrom download mode */
155 			run_command_list("rockusb 0 ${devtype} ${devnum}", -1, 0);
156 			set_back_to_bootrom_dnl_flag();
157 			do_reset(NULL, 0, 0, NULL);
158 		} else {
159 			printf("entering recovery mode!\n");
160 			env_set("reboot_mode", "recovery");
161 		}
162 	} else if (is_hotkey(HK_FASTBOOT)) {
163 		env_set("reboot_mode", "fastboot");
164 	}
165 }
166 
167 int setup_boot_mode(void)
168 {
169 	int boot_mode = BOOT_MODE_NORMAL;
170 	char env_preboot[256] = {0};
171 
172 	boot_devtype_init();
173 	rockchip_dnl_mode_check();
174 #ifdef CONFIG_RKIMG_BOOTLOADER
175 	boot_mode = rockchip_get_boot_mode();
176 #endif
177 	switch (boot_mode) {
178 	case BOOT_MODE_BOOTLOADER:
179 		printf("enter fastboot!\n");
180 #if defined(CONFIG_FASTBOOT_FLASH_MMC_DEV)
181 		snprintf(env_preboot, 256,
182 				"setenv preboot; mmc dev %x; fastboot usb 0; ",
183 				CONFIG_FASTBOOT_FLASH_MMC_DEV);
184 #elif defined(CONFIG_FASTBOOT_FLASH_NAND_DEV)
185 		snprintf(env_preboot, 256,
186 				"setenv preboot; fastboot usb 0; ");
187 #endif
188 		env_set("preboot", env_preboot);
189 		break;
190 	case BOOT_MODE_UMS:
191 		printf("enter UMS!\n");
192 		env_set("preboot", "setenv preboot; ums mmc 0");
193 		break;
194 	case BOOT_MODE_LOADER:
195 		printf("enter Rockusb!\n");
196 		env_set("preboot", "setenv preboot; rockusb 0 ${devtype} ${devnum}");
197 		break;
198 	case BOOT_MODE_CHARGING:
199 		printf("enter charging!\n");
200 		env_set("preboot", "setenv preboot; charge");
201 		break;
202 	case BOOT_MODE_RECOVERY:
203 		printf("enter Recovery mode!\n");
204 		env_set("reboot_mode", "recovery");
205 		break;
206 	}
207 
208 	return 0;
209 }
210 
211 #endif
212