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