blob: 9ccb45e6acd26e70cf3a782d428c28a95d1e4f08 [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>
Kever Yang15f09a12019-03-28 11:01:23 +08007#include <asm/arch-rockchip/bootrom.h>
8#include <asm/arch-rockchip/boot_mode.h>
Andy Yanb4d23f72017-10-11 15:00:49 +08009#include <asm/io.h>
Philipp Tomsichecfd7182017-10-10 16:21:14 +020010#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 */
17static jmp_buf brom_ctx __section(".data");
Simon Glasse1bc64e2017-04-15 13:11:31 -060018
Andy Yanb4d23f72017-10-11 15:00:49 +080019static void _back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)
20{
21 longjmp(brom_ctx, brom_cmd);
22}
23
Philipp Tomsichb82bd1f2017-10-10 16:21:16 +020024void back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)
Simon Glasse1bc64e2017-04-15 13:11:31 -060025{
Philipp Tomsich19b68fb2017-06-29 11:28:15 +020026#if CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT)
27 puts("Returning to boot ROM...\n");
Simon Glasse1bc64e2017-04-15 13:11:31 -060028#endif
Andy Yanb4d23f72017-10-11 15:00:49 +080029 _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 */
45static 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 Tomsichecfd7182017-10-10 16:21:14 +020058}
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 */
65int save_boot_params(void)
66{
67 int ret = setjmp(brom_ctx);
68
69 switch (ret) {
70 case 0:
Andy Yanb4d23f72017-10-11 15:00:49 +080071 if (check_back_to_brom_dnl_flag())
72 _back_to_bootrom(BROM_BOOT_ENTER_DNL);
Philipp Tomsichecfd7182017-10-10 16:21:14 +020073 /*
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 Yanb4d23f72017-10-11 15:00:49 +080093 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 Tomsichecfd7182017-10-10 16:21:14 +0200100 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 Glasse1bc64e2017-04-15 13:11:31 -0600108}