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