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 
12 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG == 0)
13 
14 int setup_boot_mode(void)
15 {
16 	return 0;
17 }
18 
19 #else
20 
21 void set_back_to_bootrom_dnl_flag(void)
22 {
23 	writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG);
24 }
25 
26 /*
27  * detect download key status by adc, most rockchip
28  * based boards use adc sample the download key status,
29  * but there are also some use gpio. So it's better to
30  * make this a weak function that can be override by
31  * some special boards.
32  */
33 #define KEY_DOWN_MIN_VAL	0
34 #define KEY_DOWN_MAX_VAL	30
35 
36 __weak int rockchip_dnl_key_pressed(void)
37 {
38 	unsigned int val;
39 
40 	if (adc_channel_single_shot("saradc", 1, &val)) {
41 		pr_err("%s: adc_channel_single_shot fail!\n", __func__);
42 		return false;
43 	}
44 
45 	if ((val >= KEY_DOWN_MIN_VAL) && (val <= KEY_DOWN_MAX_VAL))
46 		return true;
47 	else
48 		return false;
49 }
50 
51 void rockchip_dnl_mode_check(void)
52 {
53 	if (rockchip_dnl_key_pressed()) {
54 		printf("download key pressed, entering download mode...");
55 		set_back_to_bootrom_dnl_flag();
56 		do_reset(NULL, 0, 0, NULL);
57 	}
58 }
59 
60 int setup_boot_mode(void)
61 {
62 	void *reg = (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG;
63 	int boot_mode = readl(reg);
64 
65 	rockchip_dnl_mode_check();
66 
67 	boot_mode = readl(reg);
68 	debug("%s: boot mode 0x%08x\n", __func__, boot_mode);
69 
70 	/* Clear boot mode */
71 	writel(BOOT_NORMAL, reg);
72 
73 	switch (boot_mode) {
74 	case BOOT_FASTBOOT:
75 		debug("%s: enter fastboot!\n", __func__);
76 		env_set("preboot", "setenv preboot; fastboot usb0");
77 		break;
78 	case BOOT_UMS:
79 		debug("%s: enter UMS!\n", __func__);
80 		env_set("preboot", "setenv preboot; ums mmc 0");
81 		break;
82 	}
83 
84 	return 0;
85 }
86 
87 #endif
88