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 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG == 0) 19 20 int setup_boot_mode(void) 21 { 22 return 0; 23 } 24 25 #else 26 27 void set_back_to_bootrom_dnl_flag(void) 28 { 29 writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG); 30 } 31 32 /* 33 * detect download key status by adc, most rockchip 34 * based boards use adc sample the download key status, 35 * but there are also some use gpio. So it's better to 36 * make this a weak function that can be override by 37 * some special boards. 38 */ 39 #define KEY_DOWN_MIN_VAL 0 40 #define KEY_DOWN_MAX_VAL 30 41 42 __weak int rockchip_dnl_key_pressed(void) 43 { 44 const void *blob = gd->fdt_blob; 45 unsigned int val; 46 int channel = 1; 47 int node; 48 u32 chns[2]; 49 50 node = fdt_node_offset_by_compatible(blob, 0, "adc-keys"); 51 if (node >= 0) { 52 if (!fdtdec_get_int_array(blob, node, "io-channels", chns, 2)) 53 channel = chns[1]; 54 } 55 56 if (adc_channel_single_shot("saradc", channel, &val)) { 57 printf("%s adc_channel_single_shot fail!\n", __func__); 58 return false; 59 } 60 61 if ((val >= KEY_DOWN_MIN_VAL) && (val <= KEY_DOWN_MAX_VAL)) 62 return true; 63 else 64 return false; 65 } 66 67 static void devtype_num_envset(void) 68 { 69 const char *devtype_num_set = 70 "if mmc dev 0; then setenv devtype mmc; setenv devnum 0;" 71 "else if rknand dev 0; then setenv devtype rknand; setenv devnum 0; fi;" 72 "fi;"; 73 74 run_command_list(devtype_num_set, -1, 0); 75 } 76 77 void rockchip_dnl_mode_check(void) 78 { 79 if (rockchip_dnl_key_pressed()) { 80 printf("download key pressed, entering download mode...\n"); 81 /* If failed, we fall back to bootrom download mode */ 82 run_command_list("rockusb 0 ${devtype} ${devnum}", -1, 0); 83 set_back_to_bootrom_dnl_flag(); 84 do_reset(NULL, 0, 0, NULL); 85 } 86 } 87 88 int setup_boot_mode(void) 89 { 90 int boot_mode = BOOT_MODE_NORMAL; 91 char env_preboot[256] = {0}; 92 93 devtype_num_envset(); 94 rockchip_dnl_mode_check(); 95 #ifdef CONFIG_RKIMG_BOOTLOADER 96 boot_mode = rockchip_get_boot_mode(); 97 #endif 98 switch (boot_mode) { 99 case BOOT_MODE_BOOTLOADER: 100 printf("enter fastboot!\n"); 101 #if defined(CONFIG_FASTBOOT_FLASH_MMC_DEV) 102 snprintf(env_preboot, 256, 103 "setenv preboot; mmc dev %x; fastboot usb 0; ", 104 CONFIG_FASTBOOT_FLASH_MMC_DEV); 105 #elif defined(CONFIG_FASTBOOT_FLASH_NAND_DEV) 106 snprintf(env_preboot, 256, 107 "setenv preboot; fastboot usb 0; "); 108 #endif 109 env_set("preboot", env_preboot); 110 break; 111 case BOOT_MODE_UMS: 112 printf("enter UMS!\n"); 113 env_set("preboot", "setenv preboot; ums mmc 0"); 114 break; 115 case BOOT_MODE_LOADER: 116 printf("enter Rockusb!\n"); 117 env_set("preboot", "setenv preboot; rockusb 0 ${devtype} ${devnum}"); 118 break; 119 case BOOT_MODE_CHARGING: 120 printf("enter charging!\n"); 121 env_set("preboot", "setenv preboot; charge"); 122 break; 123 case BOOT_MODE_RECOVERY: 124 printf("enter Recovery mode!\n"); 125 env_set("reboot_mode", "recovery"); 126 break; 127 } 128 129 return 0; 130 } 131 132 #endif 133