1 // SPDX-License-Identifier: GPL-2.0+
2 /**
3  * Copyright (c) 2017 Google, Inc
4  */
5 
6 #include <common.h>
7 #include <asm/arch/bootrom.h>
8 #include <asm/arch/boot_mode.h>
9 #include <asm/io.h>
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");
18 
_back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)19 static void _back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)
20 {
21 	longjmp(brom_ctx, brom_cmd);
22 }
23 
back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)24 void back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)
25 {
26 #if CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT)
27 	puts("Returning to boot ROM...\n");
28 #endif
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  */
check_back_to_brom_dnl_flag(void)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;
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  */
save_boot_params(void)65 int save_boot_params(void)
66 {
67 	int  ret = setjmp(brom_ctx);
68 
69 	switch (ret) {
70 	case 0:
71 		if (check_back_to_brom_dnl_flag())
72 			_back_to_bootrom(BROM_BOOT_ENTER_DNL);
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;
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;
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;
108 }
109