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