Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0+ OR MIT) |
| 2 | /* |
| 3 | * Copyright (c) 2019 Amlogic, Inc. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
| 8 | #include <asm/amlogic/arch/reboot.h> |
| 9 | #include <asm/amlogic/arch/secure_apb.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <asm/amlogic/arch/bl31_apis.h> |
| 12 | #include <amlogic/partition_table.h> |
| 13 | #include <amlogic/storage.h> |
Yao Jie | 608e7b1 | 2023-11-20 17:01:54 +0800 | [diff] [blame] | 14 | #include <asm/amlogic/arch/cpu_config.h> |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 15 | #include <asm/amlogic/arch/stick_mem.h> |
| 16 | /* |
| 17 | run get_rebootmode //set reboot_mode env with current mode |
| 18 | */ |
| 19 | |
| 20 | int do_get_rebootmode (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 21 | { |
| 22 | uint32_t reboot_mode_val; |
Yao Jie | 608e7b1 | 2023-11-20 17:01:54 +0800 | [diff] [blame] | 23 | u32 stick_reboot_flag; |
| 24 | u32 bit_mask; |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 25 | |
Yao Jie | 608e7b1 | 2023-11-20 17:01:54 +0800 | [diff] [blame] | 26 | #ifdef NO_EXTEND_REBOOT_MODE |
| 27 | bit_mask = 0xf; |
| 28 | #else |
| 29 | bit_mask = 0x7f; |
| 30 | #endif |
| 31 | reboot_mode_val = ((readl(AO_SEC_SD_CFG15) >> 12) & bit_mask); |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 32 | //this step prevent the reboot mode val stored in sticky register lost |
Yao Jie | 608e7b1 | 2023-11-20 17:01:54 +0800 | [diff] [blame] | 33 | //during the exceptional reset, such as reset pin disturbance |
| 34 | stick_reboot_flag = get_stick_reboot_flag(); |
| 35 | if (reboot_mode_val == AMLOGIC_COLD_BOOT && |
| 36 | stick_reboot_flag == AMLOGIC_WATCHDOG_REBOOT) { |
| 37 | printf("Warning: system is reset abnormally!\n"); |
| 38 | printf("Set reboot_mode to watchdog reboot\n"); |
| 39 | reboot_mode_val = AMLOGIC_WATCHDOG_REBOOT; |
| 40 | } |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 41 | |
| 42 | debug("reboot_mode(0x%x)=0x%x\n", AO_SEC_SD_CFG15, reboot_mode_val); |
| 43 | |
| 44 | switch (reboot_mode_val) |
| 45 | { |
| 46 | case AMLOGIC_COLD_BOOT: |
| 47 | { |
| 48 | env_set("reboot_mode","cold_boot"); |
| 49 | break; |
| 50 | } |
| 51 | case AMLOGIC_NORMAL_BOOT: |
| 52 | { |
| 53 | env_set("reboot_mode","normal"); |
| 54 | break; |
| 55 | } |
| 56 | case AMLOGIC_FACTORY_RESET_REBOOT: |
| 57 | { |
| 58 | env_set("reboot_mode","factory_reset"); |
| 59 | break; |
| 60 | } |
| 61 | case AMLOGIC_UPDATE_REBOOT: |
| 62 | { |
| 63 | env_set("reboot_mode","update"); |
| 64 | break; |
| 65 | } |
| 66 | case AMLOGIC_FASTBOOT_REBOOT: |
| 67 | { |
| 68 | env_set("reboot_mode","fastboot"); |
| 69 | break; |
| 70 | } |
| 71 | case AMLOGIC_BOOTLOADER_REBOOT: |
| 72 | { |
| 73 | env_set("reboot_mode","bootloader"); |
| 74 | break; |
| 75 | } |
| 76 | case AMLOGIC_SUSPEND_REBOOT: |
| 77 | { |
| 78 | env_set("reboot_mode","suspend_off"); |
| 79 | break; |
| 80 | } |
| 81 | case AMLOGIC_HIBERNATE_REBOOT: |
| 82 | { |
| 83 | env_set("reboot_mode","hibernate"); |
| 84 | break; |
| 85 | } |
| 86 | case AMLOGIC_SHUTDOWN_REBOOT: |
| 87 | { |
| 88 | env_set("reboot_mode","shutdown_reboot"); |
| 89 | break; |
| 90 | } |
| 91 | case AMLOGIC_RESCUEPARTY_REBOOT: |
| 92 | { |
| 93 | env_set("reboot_mode", "rescueparty"); |
| 94 | break; |
| 95 | } |
| 96 | case AMLOGIC_KERNEL_PANIC: |
| 97 | { |
| 98 | env_set("reboot_mode","kernel_panic"); |
| 99 | break; |
| 100 | } |
| 101 | case AMLOGIC_WATCHDOG_REBOOT: |
| 102 | { |
| 103 | env_set("reboot_mode","watchdog_reboot"); |
| 104 | break; |
| 105 | } |
| 106 | case AMLOGIC_RPMBP_REBOOT: |
| 107 | { |
| 108 | env_set("reboot_mode","rpmbp"); |
| 109 | break; |
| 110 | } |
| 111 | case AMLOGIC_QUIESCENT_REBOOT: |
| 112 | { |
| 113 | env_set("reboot_mode","quiescent"); |
| 114 | break; |
| 115 | } |
| 116 | case AMLOGIC_RECOVERY_QUIESCENT_REBOOT: |
| 117 | { |
| 118 | env_set("reboot_mode","recovery_quiescent"); |
| 119 | break; |
| 120 | } |
| 121 | #ifdef AMLOGIC_FFV_REBOOT |
| 122 | case AMLOGIC_FFV_REBOOT: |
| 123 | { |
| 124 | env_set("reboot_mode", "ffv_reboot"); |
| 125 | break; |
| 126 | } |
| 127 | #endif |
| 128 | default: |
| 129 | { |
| 130 | env_set("reboot_mode","charging"); |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | #ifdef CONFIG_CMD_FASTBOOT |
| 136 | switch (reboot_mode_val) { |
| 137 | case AMLOGIC_FASTBOOT_REBOOT: { |
| 138 | env_set("reboot_mode","fastboot"); |
| 139 | break; |
| 140 | } |
| 141 | case AMLOGIC_BOOTLOADER_REBOOT: { |
Xindong Xu | d944142 | 2024-01-18 10:20:45 +0800 | [diff] [blame] | 142 | env_set("reboot_mode", "fastboot"); |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 143 | break; |
| 144 | } |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | #if defined(CONFIG_AML_RPMB) |
| 149 | run_command("rpmb_state",0); |
| 150 | #endif |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | int do_reboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 156 | { |
| 157 | uint32_t reboot_mode_val = AMLOGIC_NORMAL_BOOT; |
| 158 | if (argc <= 1) { |
| 159 | printf("reboot use default mode: normal\n"); |
| 160 | } |
| 161 | else { |
| 162 | printf("reboot mode: %s\n", argv[1]); |
| 163 | char * mode = argv[1]; |
| 164 | |
| 165 | if (strcmp(mode, "next") == 0) { |
| 166 | store_restore_bootidx(); |
Xindong Xu | 22e8daf | 2024-03-12 18:08:41 +0800 | [diff] [blame^] | 167 | reboot_mode_val = AMLOGIC_NORMAL_BOOT; |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 168 | } else if (strcmp(mode, "next,quiescent") == 0) { |
| 169 | store_restore_bootidx(); |
| 170 | reboot_mode_val = AMLOGIC_QUIESCENT_REBOOT; |
Xindong Xu | 22e8daf | 2024-03-12 18:08:41 +0800 | [diff] [blame^] | 171 | } else if (strcmp(mode, "next,bootloader") == 0) { |
| 172 | store_restore_bootidx(); |
| 173 | reboot_mode_val = AMLOGIC_BOOTLOADER_REBOOT; |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 174 | } else if (strcmp(mode, "cold_boot") == 0) |
| 175 | reboot_mode_val = AMLOGIC_COLD_BOOT; |
| 176 | else if (strcmp(mode, "normal") == 0) |
| 177 | reboot_mode_val = AMLOGIC_NORMAL_BOOT; |
Xindong Xu | d944142 | 2024-01-18 10:20:45 +0800 | [diff] [blame] | 178 | else if (strcmp(mode, "recovery") == 0) { |
| 179 | reboot_mode_val = AMLOGIC_FACTORY_RESET_REBOOT; |
| 180 | run_command("bcb recovery", 0); |
| 181 | } else if (strcmp(mode, "factory_reset") == 0) |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 182 | reboot_mode_val = AMLOGIC_FACTORY_RESET_REBOOT; |
| 183 | else if (strcmp(mode, "update") == 0) |
| 184 | reboot_mode_val = AMLOGIC_UPDATE_REBOOT; |
| 185 | else if (strcmp(mode, "fastboot") == 0) { |
| 186 | if (dynamic_partition) { |
| 187 | printf("dynamic partition, enter fastbootd"); |
| 188 | reboot_mode_val = AMLOGIC_FACTORY_RESET_REBOOT; |
| 189 | run_command("bcb fastbootd",0); |
| 190 | } else |
| 191 | reboot_mode_val = AMLOGIC_FASTBOOT_REBOOT; |
| 192 | } else if (strcmp(mode, "bootloader") == 0) |
| 193 | reboot_mode_val = AMLOGIC_BOOTLOADER_REBOOT; |
| 194 | else if (strcmp(mode, "suspend_off") == 0) |
| 195 | reboot_mode_val = AMLOGIC_SUSPEND_REBOOT; |
| 196 | else if (strcmp(mode, "hibernate") == 0) |
| 197 | reboot_mode_val = AMLOGIC_HIBERNATE_REBOOT; |
| 198 | else if (strcmp(mode, "rescueparty") == 0) |
| 199 | reboot_mode_val = AMLOGIC_RESCUEPARTY_REBOOT; |
| 200 | else if (strcmp(mode, "kernel_panic") == 0) |
| 201 | reboot_mode_val = AMLOGIC_KERNEL_PANIC; |
| 202 | else if (strcmp(mode, "rpmbp") == 0) |
| 203 | reboot_mode_val = AMLOGIC_RPMBP_REBOOT; |
| 204 | #ifdef AMLOGIC_FFV_REBOOT |
| 205 | else if (strcmp(mode, "ffv_reboot") == 0) |
| 206 | reboot_mode_val = AMLOGIC_FFV_REBOOT; |
| 207 | #endif |
| 208 | else { |
| 209 | printf("Can not find match reboot mode, use normal by default\n"); |
| 210 | reboot_mode_val = AMLOGIC_NORMAL_BOOT; |
| 211 | } |
| 212 | } |
| 213 | #ifdef CONFIG_USB_DEVICE_V2 |
| 214 | #if !(defined AML_USB_V2) |
| 215 | *P_RESET1_REGISTER |= (1<<17); |
| 216 | mdelay(200); |
| 217 | #endif |
| 218 | #endif |
| 219 | dcache_disable(); |
| 220 | |
| 221 | aml_reboot (PSCI_SYS_REBOOT, reboot_mode_val, 0, 0); |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /* USB BOOT FUNC sub command list*/ |
| 226 | #define CLEAR_USB_BOOT 1 |
| 227 | #define FORCE_USB_BOOT 2 |
| 228 | #define RUN_COMD_USB_BOOT 3 |
| 229 | #define PANIC_DUMP_USB_BOOT 4 |
| 230 | |
| 231 | int do_set_usb_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 232 | { |
| 233 | unsigned int usb_mode = 0; |
| 234 | if (argc <= 1) { |
| 235 | printf("usb flag default 0\n"); |
| 236 | } |
| 237 | else { |
| 238 | usb_mode = simple_strtoul(argv[1], NULL, 16); |
| 239 | } |
| 240 | printf("usb flag: %d\n", usb_mode); |
| 241 | set_usb_boot_function(usb_mode); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | U_BOOT_CMD( |
| 247 | get_rebootmode, 1, 0, do_get_rebootmode, |
| 248 | "get reboot mode", |
| 249 | "/N\n" |
| 250 | " This command will get and set env 'reboot_mode'\n" |
| 251 | "get_rebootmode\n" |
| 252 | ); |
| 253 | |
| 254 | U_BOOT_CMD( |
| 255 | reboot, 2, 0, do_reboot, |
| 256 | "set reboot mode and reboot system", |
| 257 | "[rebootmode]/N\n" |
| 258 | " This command will set reboot mode and reboot system\n" |
| 259 | "\n" |
| 260 | " support following [rebootmode]:\n" |
| 261 | " cold_boot\n" |
| 262 | " normal[default]\n" |
| 263 | " factory_reset/recovery\n" |
| 264 | " update\n" |
| 265 | " fastboot\n" |
| 266 | " bootloader\n" |
| 267 | " suspend_off\n" |
| 268 | " hibernate\n" |
| 269 | " next <ONLY work for SC2>\n" |
| 270 | " crash_dump\n" |
| 271 | ); |
| 272 | |
| 273 | U_BOOT_CMD( |
| 274 | set_usb_boot, 2, 0, do_set_usb_boot, |
| 275 | "set usb boot mode", |
| 276 | "[usb boot mode]/N\n" |
| 277 | " support following [usb boot mode]:\n" |
| 278 | " 1: CLEAR_USB_BOOT\n" |
| 279 | " 2: FORCE_USB_BOOT[default]\n" |
| 280 | " 3: RUN_COMD_USB_BOOT/recovery\n" |
| 281 | " 4: PANIC_DUMP_USB_BOOT\n" |
| 282 | ); |
| 283 | |
| 284 | int do_systemoff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 285 | { |
| 286 | aml_system_off(); |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | U_BOOT_CMD( |
| 292 | systemoff, 2, 1, do_systemoff, |
| 293 | "system off ", |
| 294 | "systemoff " |
| 295 | ); |