blob: b36e559e8719eba9dcfcf94609cf0e7f1cf179d0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse1bc64e2017-04-15 13:11:31 -06002/**
3 * Copyright (c) 2017 Google, Inc
Simon Glasse1bc64e2017-04-15 13:11:31 -06004 */
5
6#include <common.h>
Simon Glassdb41d652019-12-28 10:45:07 -07007#include <hang.h>
Kever Yang15f09a12019-03-28 11:01:23 +08008#include <asm/arch-rockchip/bootrom.h>
9#include <asm/arch-rockchip/boot_mode.h>
Simon Glass90526e92020-05-10 11:39:56 -060010#include <asm/cache.h>
Andy Yanb4d23f72017-10-11 15:00:49 +080011#include <asm/io.h>
Philipp Tomsichecfd7182017-10-10 16:21:14 +020012#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 */
19static jmp_buf brom_ctx __section(".data");
Simon Glasse1bc64e2017-04-15 13:11:31 -060020
Andy Yanb4d23f72017-10-11 15:00:49 +080021static void _back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)
22{
23 longjmp(brom_ctx, brom_cmd);
24}
25
Philipp Tomsichb82bd1f2017-10-10 16:21:16 +020026void back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)
Simon Glasse1bc64e2017-04-15 13:11:31 -060027{
Philipp Tomsich19b68fb2017-06-29 11:28:15 +020028#if CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT)
29 puts("Returning to boot ROM...\n");
Simon Glasse1bc64e2017-04-15 13:11:31 -060030#endif
Andy Yanb4d23f72017-10-11 15:00:49 +080031 _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 Hebb32f2ca22019-11-13 18:18:03 -080044 * relies on many default settings(such as interrupts) by
45 * itself.
Andy Yanb4d23f72017-10-11 15:00:49 +080046 */
47static 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 Tomsichecfd7182017-10-10 16:21:14 +020060}
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 */
67int save_boot_params(void)
68{
69 int ret = setjmp(brom_ctx);
70
71 switch (ret) {
72 case 0:
Andy Yanb4d23f72017-10-11 15:00:49 +080073 if (check_back_to_brom_dnl_flag())
74 _back_to_bootrom(BROM_BOOT_ENTER_DNL);
Philipp Tomsichecfd7182017-10-10 16:21:14 +020075 /*
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 Yanb4d23f72017-10-11 15:00:49 +080095 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 Tomsichecfd7182017-10-10 16:21:14 +0200102 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 Glasse1bc64e2017-04-15 13:11:31 -0600110}