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 u32 chns[2]; 50 51 node = fdt_node_offset_by_compatible(blob, 0, "adc-keys"); 52 if (node >= 0) { 53 if (!fdtdec_get_int_array(blob, node, "io-channels", chns, 2)) 54 channel = chns[1]; 55 } 56 57 if (adc_channel_single_shot("saradc", channel, &val)) { 58 printf("%s adc_channel_single_shot fail!\n", __func__); 59 return false; 60 } 61 62 if ((val >= KEY_DOWN_MIN_VAL) && (val <= KEY_DOWN_MAX_VAL)) 63 return true; 64 else 65 return false; 66 } 67 68 void devtype_num_envset(void) 69 { 70 static int done = 0; 71 72 if (done) 73 return; 74 75 const char *devtype_num_set = "run rkimg_bootdev"; 76 77 run_command_list(devtype_num_set, -1, 0); 78 done = 1; 79 } 80 81 void rockchip_dnl_mode_check(void) 82 { 83 if (rockchip_dnl_key_pressed() && rockchip_u2phy_vbus_detect()) { 84 printf("download key pressed, entering download mode...\n"); 85 /* If failed, we fall back to bootrom download mode */ 86 run_command_list("rockusb 0 ${devtype} ${devnum}", -1, 0); 87 set_back_to_bootrom_dnl_flag(); 88 do_reset(NULL, 0, 0, NULL); 89 } 90 } 91 92 int setup_boot_mode(void) 93 { 94 int boot_mode = BOOT_MODE_NORMAL; 95 char env_preboot[256] = {0}; 96 97 devtype_num_envset(); 98 rockchip_dnl_mode_check(); 99 #ifdef CONFIG_RKIMG_BOOTLOADER 100 boot_mode = rockchip_get_boot_mode(); 101 #endif 102 switch (boot_mode) { 103 case BOOT_MODE_BOOTLOADER: 104 printf("enter fastboot!\n"); 105 #if defined(CONFIG_FASTBOOT_FLASH_MMC_DEV) 106 snprintf(env_preboot, 256, 107 "setenv preboot; mmc dev %x; fastboot usb 0; ", 108 CONFIG_FASTBOOT_FLASH_MMC_DEV); 109 #elif defined(CONFIG_FASTBOOT_FLASH_NAND_DEV) 110 snprintf(env_preboot, 256, 111 "setenv preboot; fastboot usb 0; "); 112 #endif 113 env_set("preboot", env_preboot); 114 break; 115 case BOOT_MODE_UMS: 116 printf("enter UMS!\n"); 117 env_set("preboot", "setenv preboot; ums mmc 0"); 118 break; 119 case BOOT_MODE_LOADER: 120 printf("enter Rockusb!\n"); 121 env_set("preboot", "setenv preboot; rockusb 0 ${devtype} ${devnum}"); 122 break; 123 case BOOT_MODE_CHARGING: 124 printf("enter charging!\n"); 125 env_set("preboot", "setenv preboot; charge"); 126 break; 127 case BOOT_MODE_RECOVERY: 128 printf("enter Recovery mode!\n"); 129 env_set("reboot_mode", "recovery"); 130 break; 131 } 132 133 return 0; 134 } 135 136 #endif 137