1 /* 2 * (C) Copyright 2016 Rockchip Electronics Co., Ltd 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <adc.h> 9 #include <asm/io.h> 10 #include <asm/arch/boot_mode.h> 11 #include <cli.h> 12 #include <dm.h> 13 #include <fdtdec.h> 14 #include <boot_rkimg.h> 15 16 DECLARE_GLOBAL_DATA_PTR; 17 18 void set_back_to_bootrom_dnl_flag(void) 19 { 20 writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG); 21 } 22 23 /* 24 * detect download key status by adc, most rockchip 25 * based boards use adc sample the download key status, 26 * but there are also some use gpio. So it's better to 27 * make this a weak function that can be override by 28 * some special boards. 29 */ 30 #define KEY_DOWN_MIN_VAL 0 31 #define KEY_DOWN_MAX_VAL 30 32 33 __weak int rockchip_dnl_key_pressed(void) 34 { 35 const void *blob = gd->fdt_blob; 36 unsigned int val; 37 int channel = 1; 38 int node; 39 u32 chns[2]; 40 41 node = fdt_node_offset_by_compatible(blob, 0, "adc-keys"); 42 if (node >= 0) { 43 if (!fdtdec_get_int_array(blob, node, "io-channels", chns, 2)) 44 channel = chns[1]; 45 } 46 47 if (adc_channel_single_shot("saradc", channel, &val)) { 48 printf("%s adc_channel_single_shot fail!\n", __func__); 49 return false; 50 } 51 52 if ((val >= KEY_DOWN_MIN_VAL) && (val <= KEY_DOWN_MAX_VAL)) 53 return true; 54 else 55 return false; 56 } 57 58 void rockchip_dnl_mode_check(void) 59 { 60 if (rockchip_dnl_key_pressed()) { 61 printf("download key pressed, entering download mode...\n"); 62 /* If failed, we fall back to bootrom download mode */ 63 cli_simple_run_command("rockusb 0 mmc 0", 0); 64 set_back_to_bootrom_dnl_flag(); 65 do_reset(NULL, 0, 0, NULL); 66 } 67 } 68 69 int setup_boot_mode(void) 70 { 71 int boot_mode = BOOT_MODE_NORMAL; 72 char env_preboot[256] = {0}; 73 74 rockchip_dnl_mode_check(); 75 #ifdef CONFIG_RKIMG_BOOTLOADER 76 boot_mode = rockchip_get_boot_mode(); 77 #endif 78 switch (boot_mode) { 79 case BOOT_MODE_BOOTLOADER: 80 printf("enter fastboot!\n"); 81 #if defined(CONFIG_FASTBOOT_FLASH_MMC_DEV) 82 snprintf(env_preboot, 256, 83 "setenv preboot; mmc dev %x; fastboot usb 0; ", 84 CONFIG_FASTBOOT_FLASH_MMC_DEV); 85 #elif defined(CONFIG_FASTBOOT_FLASH_NAND_DEV) 86 snprintf(env_preboot, 256, 87 "setenv preboot; fastboot usb 0; "); 88 #endif 89 env_set("preboot", env_preboot); 90 break; 91 case BOOT_MODE_UMS: 92 printf("enter UMS!\n"); 93 env_set("preboot", "setenv preboot; ums mmc 0"); 94 break; 95 case BOOT_MODE_LOADER: 96 printf("enter Rockusb!\n"); 97 env_set("preboot", "setenv preboot; rockusb 0 mmc 0"); 98 break; 99 case BOOT_MODE_CHARGING: 100 printf("enter charging!\n"); 101 env_set("preboot", "setenv preboot; charge"); 102 break; 103 case BOOT_MODE_RECOVERY: 104 printf("enter Recovery mode!\n"); 105 env_set("reboot_mode", "recovery"); 106 break; 107 } 108 109 return 0; 110 } 111