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