blob: d7997d71e3001d10ae45188d567e895d260e404a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Yan70378cb2017-10-11 15:00:16 +08002/*
3 * (C) Copyright 2016 Rockchip Electronics Co., Ltd
Andy Yan70378cb2017-10-11 15:00:16 +08004 */
5
6#include <common.h>
Andy Yan215db9c2017-10-11 15:01:31 +08007#include <adc.h>
Andy Yan70378cb2017-10-11 15:00:16 +08008#include <asm/io.h>
9#include <asm/arch/boot_mode.h>
10
Philipp Tomsich3a0df082017-11-24 14:44:58 +010011#if (CONFIG_ROCKCHIP_BOOT_MODE_REG == 0)
12
13int setup_boot_mode(void)
14{
15 return 0;
16}
17
18#else
19
Andy Yan215db9c2017-10-11 15:01:31 +080020void set_back_to_bootrom_dnl_flag(void)
21{
22 writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG);
23}
24
25/*
26 * detect download key status by adc, most rockchip
27 * based boards use adc sample the download key status,
28 * but there are also some use gpio. So it's better to
29 * make this a weak function that can be override by
30 * some special boards.
31 */
32#define KEY_DOWN_MIN_VAL 0
33#define KEY_DOWN_MAX_VAL 30
34
35__weak int rockchip_dnl_key_pressed(void)
36{
37 unsigned int val;
38
39 if (adc_channel_single_shot("saradc", 1, &val)) {
40 pr_err("%s: adc_channel_single_shot fail!\n", __func__);
41 return false;
42 }
43
44 if ((val >= KEY_DOWN_MIN_VAL) && (val <= KEY_DOWN_MAX_VAL))
45 return true;
46 else
47 return false;
48}
49
50void rockchip_dnl_mode_check(void)
51{
52 if (rockchip_dnl_key_pressed()) {
53 printf("download key pressed, entering download mode...");
54 set_back_to_bootrom_dnl_flag();
55 do_reset(NULL, 0, 0, NULL);
56 }
57}
58
Andy Yan70378cb2017-10-11 15:00:16 +080059int setup_boot_mode(void)
60{
61 void *reg = (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG;
62 int boot_mode = readl(reg);
63
Simon Glass37a1f442018-12-29 06:16:41 -070064 /*
65 * This should be handled using a driver-tree property and a suitable
66 * driver which can read the appropriate settings. As it is, this
67 * breaks chromebook_minnie.\
68 *
69 * rockchip_dnl_mode_check();
70 */
Andy Yan215db9c2017-10-11 15:01:31 +080071
72 boot_mode = readl(reg);
73 debug("%s: boot mode 0x%08x\n", __func__, boot_mode);
Andy Yan70378cb2017-10-11 15:00:16 +080074
75 /* Clear boot mode */
76 writel(BOOT_NORMAL, reg);
77
78 switch (boot_mode) {
79 case BOOT_FASTBOOT:
Andy Yan215db9c2017-10-11 15:01:31 +080080 debug("%s: enter fastboot!\n", __func__);
Andy Yan70378cb2017-10-11 15:00:16 +080081 env_set("preboot", "setenv preboot; fastboot usb0");
82 break;
83 case BOOT_UMS:
Andy Yan215db9c2017-10-11 15:01:31 +080084 debug("%s: enter UMS!\n", __func__);
Andy Yan70378cb2017-10-11 15:00:16 +080085 env_set("preboot", "setenv preboot; ums mmc 0");
86 break;
87 }
88
89 return 0;
90}
Philipp Tomsich3a0df082017-11-24 14:44:58 +010091
92#endif