Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e1bc64e | 2017-04-15 13:11:31 -0600 | [diff] [blame] | 2 | /** |
| 3 | * Copyright (c) 2017 Google, Inc |
Simon Glass | e1bc64e | 2017-04-15 13:11:31 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | db41d65 | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 7 | #include <hang.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 8 | #include <asm/arch-rockchip/bootrom.h> |
| 9 | #include <asm/arch-rockchip/boot_mode.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 10 | #include <asm/cache.h> |
Andy Yan | b4d23f7 | 2017-10-11 15:00:49 +0800 | [diff] [blame] | 11 | #include <asm/io.h> |
Philipp Tomsich | ecfd718 | 2017-10-10 16:21:14 +0200 | [diff] [blame] | 12 | #include <asm/setjmp.h> |
| 13 | #include <asm/system.h> |
| 14 | |
| 15 | /* |
| 16 | * Force the jmp_buf to the data-section, as .bss will not be valid |
| 17 | * when save_boot_params is invoked. |
| 18 | */ |
| 19 | static jmp_buf brom_ctx __section(".data"); |
Simon Glass | e1bc64e | 2017-04-15 13:11:31 -0600 | [diff] [blame] | 20 | |
Andy Yan | b4d23f7 | 2017-10-11 15:00:49 +0800 | [diff] [blame] | 21 | static void _back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd) |
| 22 | { |
| 23 | longjmp(brom_ctx, brom_cmd); |
| 24 | } |
| 25 | |
Philipp Tomsich | b82bd1f | 2017-10-10 16:21:16 +0200 | [diff] [blame] | 26 | void back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd) |
Simon Glass | e1bc64e | 2017-04-15 13:11:31 -0600 | [diff] [blame] | 27 | { |
Philipp Tomsich | 19b68fb | 2017-06-29 11:28:15 +0200 | [diff] [blame] | 28 | #if CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT) |
| 29 | puts("Returning to boot ROM...\n"); |
Simon Glass | e1bc64e | 2017-04-15 13:11:31 -0600 | [diff] [blame] | 30 | #endif |
Andy Yan | b4d23f7 | 2017-10-11 15:00:49 +0800 | [diff] [blame] | 31 | _back_to_bootrom(brom_cmd); |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | * we back to bootrom download mode if get a |
| 36 | * BOOT_BROM_DOWNLOAD flag in boot mode register |
| 37 | * |
| 38 | * note: the boot mode register is configured by |
| 39 | * application(next stage bootloader, kernel, etc), |
| 40 | * and the bootrom never check this register, so we need |
| 41 | * to check it and back to bootrom at very early bootstage(before |
| 42 | * some basic configurations(such as interrupts) been |
| 43 | * changed by TPL/SPL, as the bootrom download operation |
Thomas Hebb | 32f2ca2 | 2019-11-13 18:18:03 -0800 | [diff] [blame] | 44 | * relies on many default settings(such as interrupts) by |
| 45 | * itself. |
Andy Yan | b4d23f7 | 2017-10-11 15:00:49 +0800 | [diff] [blame] | 46 | */ |
| 47 | static bool check_back_to_brom_dnl_flag(void) |
| 48 | { |
| 49 | u32 boot_mode; |
| 50 | |
| 51 | if (CONFIG_ROCKCHIP_BOOT_MODE_REG) { |
| 52 | boot_mode = readl(CONFIG_ROCKCHIP_BOOT_MODE_REG); |
| 53 | if (boot_mode == BOOT_BROM_DOWNLOAD) { |
| 54 | writel(0, CONFIG_ROCKCHIP_BOOT_MODE_REG); |
| 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return false; |
Philipp Tomsich | ecfd718 | 2017-10-10 16:21:14 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* |
| 63 | * All Rockchip BROM implementations enter with a valid stack-pointer, |
| 64 | * so this can safely be implemented in C (providing a single |
| 65 | * implementation both for ARMv7 and AArch64). |
| 66 | */ |
| 67 | int save_boot_params(void) |
| 68 | { |
| 69 | int ret = setjmp(brom_ctx); |
| 70 | |
| 71 | switch (ret) { |
| 72 | case 0: |
Andy Yan | b4d23f7 | 2017-10-11 15:00:49 +0800 | [diff] [blame] | 73 | if (check_back_to_brom_dnl_flag()) |
| 74 | _back_to_bootrom(BROM_BOOT_ENTER_DNL); |
Philipp Tomsich | ecfd718 | 2017-10-10 16:21:14 +0200 | [diff] [blame] | 75 | /* |
| 76 | * This is the initial pass through this function |
| 77 | * (i.e. saving the context), setjmp just setup up the |
| 78 | * brom_ctx: transfer back into the startup-code at |
| 79 | * 'save_boot_params_ret' and let the compiler know |
| 80 | * that this will not return. |
| 81 | */ |
| 82 | save_boot_params_ret(); |
| 83 | while (true) |
| 84 | /* does not return */; |
| 85 | break; |
| 86 | |
| 87 | case BROM_BOOT_NEXTSTAGE: |
| 88 | /* |
| 89 | * To instruct the BROM to boot the next stage, we |
| 90 | * need to return 0 to it: i.e. we need to rewrite |
| 91 | * the return code once more. |
| 92 | */ |
| 93 | ret = 0; |
| 94 | break; |
Andy Yan | b4d23f7 | 2017-10-11 15:00:49 +0800 | [diff] [blame] | 95 | case BROM_BOOT_ENTER_DNL: |
| 96 | /* |
| 97 | * A non-zero return value will instruct the BROM enter |
| 98 | * download mode. |
| 99 | */ |
| 100 | ret = 1; |
| 101 | break; |
Philipp Tomsich | ecfd718 | 2017-10-10 16:21:14 +0200 | [diff] [blame] | 102 | default: |
| 103 | #if CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT) |
| 104 | puts("FATAL: unexpected command to back_to_bootrom()\n"); |
| 105 | #endif |
| 106 | hang(); |
| 107 | }; |
| 108 | |
| 109 | return ret; |
Simon Glass | e1bc64e | 2017-04-15 13:11:31 -0600 | [diff] [blame] | 110 | } |