blob: d7dd425aaef7b8d1e767304f1aeba66f2007a150 [file] [log] [blame]
Andy Yan70378cb2017-10-11 15:00:16 +08001/*
2 * (C) Copyright 2016 Rockchip Electronics Co., Ltd
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
Andy Yan215db9c2017-10-11 15:01:31 +08008#include <adc.h>
Andy Yan70378cb2017-10-11 15:00:16 +08009#include <asm/io.h>
10#include <asm/arch/boot_mode.h>
11
Philipp Tomsich3a0df082017-11-24 14:44:58 +010012#if (CONFIG_ROCKCHIP_BOOT_MODE_REG == 0)
13
14int setup_boot_mode(void)
15{
16 return 0;
17}
18
19#else
20
Andy Yan215db9c2017-10-11 15:01:31 +080021void 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
51void 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
Andy Yan70378cb2017-10-11 15:00:16 +080060int setup_boot_mode(void)
61{
62 void *reg = (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG;
63 int boot_mode = readl(reg);
64
Andy Yan215db9c2017-10-11 15:01:31 +080065 rockchip_dnl_mode_check();
66
67 boot_mode = readl(reg);
68 debug("%s: boot mode 0x%08x\n", __func__, boot_mode);
Andy Yan70378cb2017-10-11 15:00:16 +080069
70 /* Clear boot mode */
71 writel(BOOT_NORMAL, reg);
72
73 switch (boot_mode) {
74 case BOOT_FASTBOOT:
Andy Yan215db9c2017-10-11 15:01:31 +080075 debug("%s: enter fastboot!\n", __func__);
Andy Yan70378cb2017-10-11 15:00:16 +080076 env_set("preboot", "setenv preboot; fastboot usb0");
77 break;
78 case BOOT_UMS:
Andy Yan215db9c2017-10-11 15:01:31 +080079 debug("%s: enter UMS!\n", __func__);
Andy Yan70378cb2017-10-11 15:00:16 +080080 env_set("preboot", "setenv preboot; ums mmc 0");
81 break;
82 }
83
84 return 0;
85}
Philipp Tomsich3a0df082017-11-24 14:44:58 +010086
87#endif