blob: eb8f65ae4e9d76a1b221fcdf32a83d91d3f2939a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Yane3067792017-10-11 15:00:16 +08002/*
3 * (C) Copyright 2016 Rockchip Electronics Co., Ltd
Andy Yane3067792017-10-11 15:00:16 +08004 */
5
6#include <common.h>
Andy Yanecb103b2017-10-11 15:01:31 +08007#include <adc.h>
Simon Glass09140112020-05-10 11:40:03 -06008#include <command.h>
9#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Andy Yane3067792017-10-11 15:00:16 +080011#include <asm/io.h>
Kever Yang15f09a12019-03-28 11:01:23 +080012#include <asm/arch-rockchip/boot_mode.h>
Hugh Cole-Baker80b98822020-01-25 16:19:36 +000013#include <dm/device.h>
14#include <dm/uclass.h>
Simon Glass1e94b462023-09-14 18:21:46 -060015#include <linux/printk.h>
Andy Yane3067792017-10-11 15:00:16 +080016
Philipp Tomsichf07d76c2017-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 Yanecb103b2017-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{
43 unsigned int val;
Hugh Cole-Baker80b98822020-01-25 16:19:36 +000044 struct udevice *dev;
45 struct uclass *uc;
46 int ret;
Andy Yanecb103b2017-10-11 15:01:31 +080047
Hugh Cole-Baker80b98822020-01-25 16:19:36 +000048 ret = uclass_get(UCLASS_ADC, &uc);
49 if (ret)
50 return false;
51
52 ret = -ENODEV;
53 uclass_foreach_dev(dev, uc) {
54 if (!strncmp(dev->name, "saradc", 6)) {
55 ret = adc_channel_single_shot(dev->name, 1, &val);
56 break;
57 }
58 }
59
60 if (ret == -ENODEV) {
61 pr_warn("%s: no saradc device found\n", __func__);
62 return false;
63 } else if (ret) {
Andy Yanecb103b2017-10-11 15:01:31 +080064 pr_err("%s: adc_channel_single_shot fail!\n", __func__);
65 return false;
66 }
67
68 if ((val >= KEY_DOWN_MIN_VAL) && (val <= KEY_DOWN_MAX_VAL))
69 return true;
70 else
71 return false;
72}
73
74void rockchip_dnl_mode_check(void)
75{
76 if (rockchip_dnl_key_pressed()) {
77 printf("download key pressed, entering download mode...");
78 set_back_to_bootrom_dnl_flag();
79 do_reset(NULL, 0, 0, NULL);
80 }
81}
82
Andy Yane3067792017-10-11 15:00:16 +080083int setup_boot_mode(void)
84{
85 void *reg = (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG;
86 int boot_mode = readl(reg);
87
Philipp Tomsicha2893ba2019-03-29 09:21:13 +010088 rockchip_dnl_mode_check();
Andy Yanecb103b2017-10-11 15:01:31 +080089
90 boot_mode = readl(reg);
91 debug("%s: boot mode 0x%08x\n", __func__, boot_mode);
Andy Yane3067792017-10-11 15:00:16 +080092
93 /* Clear boot mode */
94 writel(BOOT_NORMAL, reg);
95
96 switch (boot_mode) {
97 case BOOT_FASTBOOT:
Andy Yanecb103b2017-10-11 15:01:31 +080098 debug("%s: enter fastboot!\n", __func__);
John Keepingeaee44a2021-11-25 18:05:22 +000099 env_set("preboot", "setenv preboot; fastboot usb 0");
Andy Yane3067792017-10-11 15:00:16 +0800100 break;
101 case BOOT_UMS:
Andy Yanecb103b2017-10-11 15:01:31 +0800102 debug("%s: enter UMS!\n", __func__);
Andy Yane3067792017-10-11 15:00:16 +0800103 env_set("preboot", "setenv preboot; ums mmc 0");
104 break;
105 }
106
107 return 0;
108}
Philipp Tomsichf07d76c2017-11-24 14:44:58 +0100109
110#endif