command: Remove the cmd_tbl_t typedef
[oweals/u-boot.git] / arch / arm / mach-rockchip / boot_mode.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2016 Rockchip Electronics Co., Ltd
4  */
5
6 #include <common.h>
7 #include <adc.h>
8 #include <command.h>
9 #include <env.h>
10 #include <asm/io.h>
11 #include <asm/arch-rockchip/boot_mode.h>
12 #include <dm/device.h>
13 #include <dm/uclass.h>
14
15 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG == 0)
16
17 int setup_boot_mode(void)
18 {
19         return 0;
20 }
21
22 #else
23
24 void set_back_to_bootrom_dnl_flag(void)
25 {
26         writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG);
27 }
28
29 /*
30  * detect download key status by adc, most rockchip
31  * based boards use adc sample the download key status,
32  * but there are also some use gpio. So it's better to
33  * make this a weak function that can be override by
34  * some special boards.
35  */
36 #define KEY_DOWN_MIN_VAL        0
37 #define KEY_DOWN_MAX_VAL        30
38
39 __weak int rockchip_dnl_key_pressed(void)
40 {
41         unsigned int val;
42         struct udevice *dev;
43         struct uclass *uc;
44         int ret;
45
46         ret = uclass_get(UCLASS_ADC, &uc);
47         if (ret)
48                 return false;
49
50         ret = -ENODEV;
51         uclass_foreach_dev(dev, uc) {
52                 if (!strncmp(dev->name, "saradc", 6)) {
53                         ret = adc_channel_single_shot(dev->name, 1, &val);
54                         break;
55                 }
56         }
57
58         if (ret == -ENODEV) {
59                 pr_warn("%s: no saradc device found\n", __func__);
60                 return false;
61         } else if (ret) {
62                 pr_err("%s: adc_channel_single_shot fail!\n", __func__);
63                 return false;
64         }
65
66         if ((val >= KEY_DOWN_MIN_VAL) && (val <= KEY_DOWN_MAX_VAL))
67                 return true;
68         else
69                 return false;
70 }
71
72 void rockchip_dnl_mode_check(void)
73 {
74         if (rockchip_dnl_key_pressed()) {
75                 printf("download key pressed, entering download mode...");
76                 set_back_to_bootrom_dnl_flag();
77                 do_reset(NULL, 0, 0, NULL);
78         }
79 }
80
81 int setup_boot_mode(void)
82 {
83         void *reg = (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG;
84         int boot_mode = readl(reg);
85
86         rockchip_dnl_mode_check();
87
88         boot_mode = readl(reg);
89         debug("%s: boot mode 0x%08x\n", __func__, boot_mode);
90
91         /* Clear boot mode */
92         writel(BOOT_NORMAL, reg);
93
94         switch (boot_mode) {
95         case BOOT_FASTBOOT:
96                 debug("%s: enter fastboot!\n", __func__);
97                 env_set("preboot", "setenv preboot; fastboot usb0");
98                 break;
99         case BOOT_UMS:
100                 debug("%s: enter UMS!\n", __func__);
101                 env_set("preboot", "setenv preboot; ums mmc 0");
102                 break;
103         }
104
105         return 0;
106 }
107
108 #endif