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