blob: f9be396aa558f8fb560e8d9b766d9396fe087028 [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>
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
9#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Andy Yan70378cb2017-10-11 15:00:16 +080011#include <asm/io.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080012#include <asm/arch-rockchip/boot_mode.h>
Hugh Cole-Baker82820942020-01-25 16:19:36 +000013#include <dm/device.h>
14#include <dm/uclass.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060015#include <linux/printk.h>
Andy Yan70378cb2017-10-11 15:00:16 +080016
Philipp Tomsich3a0df082017-11-24 14:44:58 +010017#if (CONFIG_ROCKCHIP_BOOT_MODE_REG == 0)
18
19int setup_boot_mode(void)
20{
21 return 0;
22}
23
24#else
25
Andy Yan215db9c2017-10-11 15:01:31 +080026void set_back_to_bootrom_dnl_flag(void)
27{
28 writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG);
29}
30
31/*
32 * detect download key status by adc, most rockchip
33 * based boards use adc sample the download key status,
34 * but there are also some use gpio. So it's better to
35 * make this a weak function that can be override by
36 * some special boards.
37 */
38#define KEY_DOWN_MIN_VAL 0
39#define KEY_DOWN_MAX_VAL 30
40
41__weak int rockchip_dnl_key_pressed(void)
42{
Quentin Schulzcc56d922024-03-14 10:36:29 +010043#if CONFIG_IS_ENABLED(ADC)
Andy Yan215db9c2017-10-11 15:01:31 +080044 unsigned int val;
Hugh Cole-Baker82820942020-01-25 16:19:36 +000045 struct udevice *dev;
46 struct uclass *uc;
47 int ret;
48
49 ret = uclass_get(UCLASS_ADC, &uc);
50 if (ret)
51 return false;
52
53 ret = -ENODEV;
54 uclass_foreach_dev(dev, uc) {
55 if (!strncmp(dev->name, "saradc", 6)) {
56 ret = adc_channel_single_shot(dev->name, 1, &val);
57 break;
58 }
59 }
Andy Yan215db9c2017-10-11 15:01:31 +080060
Hugh Cole-Baker82820942020-01-25 16:19:36 +000061 if (ret == -ENODEV) {
62 pr_warn("%s: no saradc device found\n", __func__);
63 return false;
64 } else if (ret) {
Andy Yan215db9c2017-10-11 15:01:31 +080065 pr_err("%s: adc_channel_single_shot fail!\n", __func__);
66 return false;
67 }
68
69 if ((val >= KEY_DOWN_MIN_VAL) && (val <= KEY_DOWN_MAX_VAL))
70 return true;
71 else
72 return false;
Quentin Schulzcc56d922024-03-14 10:36:29 +010073#else
74 return false;
75#endif
Andy Yan215db9c2017-10-11 15:01:31 +080076}
77
78void rockchip_dnl_mode_check(void)
79{
80 if (rockchip_dnl_key_pressed()) {
81 printf("download key pressed, entering download mode...");
82 set_back_to_bootrom_dnl_flag();
83 do_reset(NULL, 0, 0, NULL);
84 }
85}
86
Andy Yan70378cb2017-10-11 15:00:16 +080087int setup_boot_mode(void)
88{
89 void *reg = (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG;
90 int boot_mode = readl(reg);
91
Philipp Tomsiche91b8452019-03-29 09:21:13 +010092 rockchip_dnl_mode_check();
Andy Yan215db9c2017-10-11 15:01:31 +080093
94 boot_mode = readl(reg);
95 debug("%s: boot mode 0x%08x\n", __func__, boot_mode);
Andy Yan70378cb2017-10-11 15:00:16 +080096
97 /* Clear boot mode */
98 writel(BOOT_NORMAL, reg);
99
100 switch (boot_mode) {
101 case BOOT_FASTBOOT:
Andy Yan215db9c2017-10-11 15:01:31 +0800102 debug("%s: enter fastboot!\n", __func__);
John Keeping1a76b3a2021-11-25 18:05:22 +0000103 env_set("preboot", "setenv preboot; fastboot usb 0");
Andy Yan70378cb2017-10-11 15:00:16 +0800104 break;
105 case BOOT_UMS:
Andy Yan215db9c2017-10-11 15:01:31 +0800106 debug("%s: enter UMS!\n", __func__);
Andy Yan70378cb2017-10-11 15:00:16 +0800107 env_set("preboot", "setenv preboot; ums mmc 0");
108 break;
109 }
110
111 return 0;
112}
Philipp Tomsich3a0df082017-11-24 14:44:58 +0100113
114#endif