Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andy Yan | 70378cb | 2017-10-11 15:00:16 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 Rockchip Electronics Co., Ltd |
Andy Yan | 70378cb | 2017-10-11 15:00:16 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Andy Yan | 215db9c | 2017-10-11 15:01:31 +0800 | [diff] [blame] | 7 | #include <adc.h> |
Andy Yan | 70378cb | 2017-10-11 15:00:16 +0800 | [diff] [blame] | 8 | #include <asm/io.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 9 | #include <asm/arch-rockchip/boot_mode.h> |
Hugh Cole-Baker | 8282094 | 2020-01-25 16:19:36 +0000 | [diff] [blame] | 10 | #include <dm/device.h> |
| 11 | #include <dm/uclass.h> |
Andy Yan | 70378cb | 2017-10-11 15:00:16 +0800 | [diff] [blame] | 12 | |
Philipp Tomsich | 3a0df08 | 2017-11-24 14:44:58 +0100 | [diff] [blame] | 13 | #if (CONFIG_ROCKCHIP_BOOT_MODE_REG == 0) |
| 14 | |
| 15 | int setup_boot_mode(void) |
| 16 | { |
| 17 | return 0; |
| 18 | } |
| 19 | |
| 20 | #else |
| 21 | |
Andy Yan | 215db9c | 2017-10-11 15:01:31 +0800 | [diff] [blame] | 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; |
Hugh Cole-Baker | 8282094 | 2020-01-25 16:19:36 +0000 | [diff] [blame] | 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 | } |
Andy Yan | 215db9c | 2017-10-11 15:01:31 +0800 | [diff] [blame] | 55 | |
Hugh Cole-Baker | 8282094 | 2020-01-25 16:19:36 +0000 | [diff] [blame] | 56 | if (ret == -ENODEV) { |
| 57 | pr_warn("%s: no saradc device found\n", __func__); |
| 58 | return false; |
| 59 | } else if (ret) { |
Andy Yan | 215db9c | 2017-10-11 15:01:31 +0800 | [diff] [blame] | 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 | |
Andy Yan | 70378cb | 2017-10-11 15:00:16 +0800 | [diff] [blame] | 79 | int setup_boot_mode(void) |
| 80 | { |
| 81 | void *reg = (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG; |
| 82 | int boot_mode = readl(reg); |
| 83 | |
Philipp Tomsich | e91b845 | 2019-03-29 09:21:13 +0100 | [diff] [blame] | 84 | rockchip_dnl_mode_check(); |
Andy Yan | 215db9c | 2017-10-11 15:01:31 +0800 | [diff] [blame] | 85 | |
| 86 | boot_mode = readl(reg); |
| 87 | debug("%s: boot mode 0x%08x\n", __func__, boot_mode); |
Andy Yan | 70378cb | 2017-10-11 15:00:16 +0800 | [diff] [blame] | 88 | |
| 89 | /* Clear boot mode */ |
| 90 | writel(BOOT_NORMAL, reg); |
| 91 | |
| 92 | switch (boot_mode) { |
| 93 | case BOOT_FASTBOOT: |
Andy Yan | 215db9c | 2017-10-11 15:01:31 +0800 | [diff] [blame] | 94 | debug("%s: enter fastboot!\n", __func__); |
Andy Yan | 70378cb | 2017-10-11 15:00:16 +0800 | [diff] [blame] | 95 | env_set("preboot", "setenv preboot; fastboot usb0"); |
| 96 | break; |
| 97 | case BOOT_UMS: |
Andy Yan | 215db9c | 2017-10-11 15:01:31 +0800 | [diff] [blame] | 98 | debug("%s: enter UMS!\n", __func__); |
Andy Yan | 70378cb | 2017-10-11 15:00:16 +0800 | [diff] [blame] | 99 | env_set("preboot", "setenv preboot; ums mmc 0"); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
Philipp Tomsich | 3a0df08 | 2017-11-24 14:44:58 +0100 | [diff] [blame] | 105 | |
| 106 | #endif |