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