blob: 1524eca27299c9c2964c59469edd3696a6a05ec8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassef0ab852017-04-15 13:11:31 -06002/**
3 * Copyright (c) 2017 Google, Inc
Simon Glassef0ab852017-04-15 13:11:31 -06004 */
5
6#include <common.h>
Simon Glassf11478f2019-12-28 10:45:07 -07007#include <hang.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +08008#include <asm/arch-rockchip/bootrom.h>
9#include <asm/arch-rockchip/boot_mode.h>
Andy Yand2349d92017-10-11 15:00:49 +080010#include <asm/io.h>
Philipp Tomsich39bf18d2017-10-10 16:21:14 +020011#include <asm/setjmp.h>
12#include <asm/system.h>
13
14/*
15 * Force the jmp_buf to the data-section, as .bss will not be valid
16 * when save_boot_params is invoked.
17 */
18static jmp_buf brom_ctx __section(".data");
Simon Glassef0ab852017-04-15 13:11:31 -060019
Andy Yand2349d92017-10-11 15:00:49 +080020static void _back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)
21{
22 longjmp(brom_ctx, brom_cmd);
23}
24
Philipp Tomsich7234c732017-10-10 16:21:16 +020025void back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)
Simon Glassef0ab852017-04-15 13:11:31 -060026{
Philipp Tomsich9b5545d2017-06-29 11:28:15 +020027#if CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT)
28 puts("Returning to boot ROM...\n");
Simon Glassef0ab852017-04-15 13:11:31 -060029#endif
Andy Yand2349d92017-10-11 15:00:49 +080030 _back_to_bootrom(brom_cmd);
31}
32
33/*
34 * we back to bootrom download mode if get a
35 * BOOT_BROM_DOWNLOAD flag in boot mode register
36 *
37 * note: the boot mode register is configured by
38 * application(next stage bootloader, kernel, etc),
39 * and the bootrom never check this register, so we need
40 * to check it and back to bootrom at very early bootstage(before
41 * some basic configurations(such as interrupts) been
42 * changed by TPL/SPL, as the bootrom download operation
Thomas Hebbfd37f242019-11-13 18:18:03 -080043 * relies on many default settings(such as interrupts) by
44 * itself.
Andy Yand2349d92017-10-11 15:00:49 +080045 */
46static bool check_back_to_brom_dnl_flag(void)
47{
48 u32 boot_mode;
49
50 if (CONFIG_ROCKCHIP_BOOT_MODE_REG) {
51 boot_mode = readl(CONFIG_ROCKCHIP_BOOT_MODE_REG);
52 if (boot_mode == BOOT_BROM_DOWNLOAD) {
53 writel(0, CONFIG_ROCKCHIP_BOOT_MODE_REG);
54 return true;
55 }
56 }
57
58 return false;
Philipp Tomsich39bf18d2017-10-10 16:21:14 +020059}
60
61/*
62 * All Rockchip BROM implementations enter with a valid stack-pointer,
63 * so this can safely be implemented in C (providing a single
64 * implementation both for ARMv7 and AArch64).
65 */
66int save_boot_params(void)
67{
68 int ret = setjmp(brom_ctx);
69
70 switch (ret) {
71 case 0:
Andy Yand2349d92017-10-11 15:00:49 +080072 if (check_back_to_brom_dnl_flag())
73 _back_to_bootrom(BROM_BOOT_ENTER_DNL);
Philipp Tomsich39bf18d2017-10-10 16:21:14 +020074 /*
75 * This is the initial pass through this function
76 * (i.e. saving the context), setjmp just setup up the
77 * brom_ctx: transfer back into the startup-code at
78 * 'save_boot_params_ret' and let the compiler know
79 * that this will not return.
80 */
81 save_boot_params_ret();
82 while (true)
83 /* does not return */;
84 break;
85
86 case BROM_BOOT_NEXTSTAGE:
87 /*
88 * To instruct the BROM to boot the next stage, we
89 * need to return 0 to it: i.e. we need to rewrite
90 * the return code once more.
91 */
92 ret = 0;
93 break;
Andy Yand2349d92017-10-11 15:00:49 +080094 case BROM_BOOT_ENTER_DNL:
95 /*
96 * A non-zero return value will instruct the BROM enter
97 * download mode.
98 */
99 ret = 1;
100 break;
Philipp Tomsich39bf18d2017-10-10 16:21:14 +0200101 default:
102#if CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT)
103 puts("FATAL: unexpected command to back_to_bootrom()\n");
104#endif
105 hang();
106 };
107
108 return ret;
Simon Glassef0ab852017-04-15 13:11:31 -0600109}