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