1 /**
2 * Copyright (c) 2017 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7 #include <common.h>
8 #include <asm/arch/bootrom.h>
9 #include <asm/arch/boot_mode.h>
10 #include <asm/io.h>
11 #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 */
18 static jmp_buf brom_ctx __section(".data");
19
_back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)20 static void _back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)
21 {
22 longjmp(brom_ctx, brom_cmd);
23 }
24
back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)25 void back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)
26 {
27 #if CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT)
28 puts("Returning to boot ROM...\n");
29 #endif
30 _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 * The bootrom never check this register, so we need
38 * to check it and back to bootrom at very early bootstage(before
39 * some basic configurations(such as interrupts) been
40 * changed by TPL/SPL, as the bootrom download operation
41 * relys on many default settings(such as interrupts) by
42 * it's self.
43 * Note: the boot mode register is configured by
44 * application(next stage bootloader, kernel, etc) via command or PC Tool,
45 * cleared by USB download(bootrom mode) or loader(other mode) after the
46 * tag has work.
47 */
check_back_to_brom_dnl_flag(void)48 static bool check_back_to_brom_dnl_flag(void)
49 {
50 u32 boot_mode, boot_id;
51
52 if (CONFIG_ROCKCHIP_BOOT_MODE_REG && BROM_BOOTSOURCE_ID_ADDR) {
53 boot_mode = readl(CONFIG_ROCKCHIP_BOOT_MODE_REG);
54 boot_id = readl(BROM_BOOTSOURCE_ID_ADDR);
55 if (boot_id == BROM_BOOTSOURCE_USB)
56 writel(0, CONFIG_ROCKCHIP_BOOT_MODE_REG);
57 else if (boot_mode == BOOT_BROM_DOWNLOAD)
58 return true;
59 }
60
61 return false;
62 }
63
64 /*
65 * All Rockchip BROM implementations enter with a valid stack-pointer,
66 * so this can safely be implemented in C (providing a single
67 * implementation both for ARMv7 and AArch64).
68 */
save_boot_params(void)69 int save_boot_params(void)
70 {
71 int ret = setjmp(brom_ctx);
72
73 switch (ret) {
74 case 0:
75 if (check_back_to_brom_dnl_flag())
76 _back_to_bootrom(BROM_BOOT_ENTER_DNL);
77 /*
78 * This is the initial pass through this function
79 * (i.e. saving the context), setjmp just setup up the
80 * brom_ctx: transfer back into the startup-code at
81 * 'save_boot_params_ret' and let the compiler know
82 * that this will not return.
83 */
84 save_boot_params_ret();
85 while (true)
86 /* does not return */;
87 break;
88
89 case BROM_BOOT_NEXTSTAGE:
90 /*
91 * To instruct the BROM to boot the next stage, we
92 * need to return 0 to it: i.e. we need to rewrite
93 * the return code once more.
94 */
95 ret = 0;
96 break;
97 case BROM_BOOT_ENTER_DNL:
98 /*
99 * A non-zero return value will instruct the BROM enter
100 * download mode.
101 */
102 ret = 1;
103 break;
104 default:
105 #if CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT)
106 puts("FATAL: unexpected command to back_to_bootrom()\n");
107 #endif
108 hang();
109 };
110
111 return ret;
112 }
113