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