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>
947a9f8fcSAndy Yan #include <asm/arch/boot_mode.h>
1047a9f8fcSAndy Yan #include <asm/io.h>
113513fb1eSPhilipp Tomsich #include <asm/setjmp.h>
123513fb1eSPhilipp Tomsich #include <asm/system.h>
133513fb1eSPhilipp Tomsich
143513fb1eSPhilipp Tomsich /*
153513fb1eSPhilipp Tomsich * Force the jmp_buf to the data-section, as .bss will not be valid
163513fb1eSPhilipp Tomsich * when save_boot_params is invoked.
173513fb1eSPhilipp Tomsich */
183513fb1eSPhilipp Tomsich static jmp_buf brom_ctx __section(".data");
19e1bc64eeSSimon Glass
_back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)2047a9f8fcSAndy Yan static void _back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)
2147a9f8fcSAndy Yan {
2247a9f8fcSAndy Yan longjmp(brom_ctx, brom_cmd);
2347a9f8fcSAndy Yan }
2447a9f8fcSAndy Yan
back_to_bootrom(enum rockchip_bootrom_cmd brom_cmd)2550b28820SPhilipp 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
3047a9f8fcSAndy Yan _back_to_bootrom(brom_cmd);
3147a9f8fcSAndy Yan }
3247a9f8fcSAndy Yan
3347a9f8fcSAndy Yan /*
34*ca0ddd93SKever Yang * We back to bootrom download mode if get a
3547a9f8fcSAndy Yan * BOOT_BROM_DOWNLOAD flag in boot mode register
3647a9f8fcSAndy Yan *
37*ca0ddd93SKever Yang * The bootrom never check this register, so we need
3847a9f8fcSAndy Yan * to check it and back to bootrom at very early bootstage(before
3947a9f8fcSAndy Yan * some basic configurations(such as interrupts) been
4047a9f8fcSAndy Yan * changed by TPL/SPL, as the bootrom download operation
4147a9f8fcSAndy Yan * relys on many default settings(such as interrupts) by
4247a9f8fcSAndy Yan * it's self.
43*ca0ddd93SKever Yang * Note: the boot mode register is configured by
44*ca0ddd93SKever Yang * application(next stage bootloader, kernel, etc) via command or PC Tool,
45*ca0ddd93SKever Yang * cleared by USB download(bootrom mode) or loader(other mode) after the
46*ca0ddd93SKever Yang * tag has work.
4747a9f8fcSAndy Yan */
check_back_to_brom_dnl_flag(void)4847a9f8fcSAndy Yan static bool check_back_to_brom_dnl_flag(void)
4947a9f8fcSAndy Yan {
50*ca0ddd93SKever Yang u32 boot_mode, boot_id;
5147a9f8fcSAndy Yan
52*ca0ddd93SKever Yang if (CONFIG_ROCKCHIP_BOOT_MODE_REG && BROM_BOOTSOURCE_ID_ADDR) {
5347a9f8fcSAndy Yan boot_mode = readl(CONFIG_ROCKCHIP_BOOT_MODE_REG);
54*ca0ddd93SKever Yang boot_id = readl(BROM_BOOTSOURCE_ID_ADDR);
55*ca0ddd93SKever Yang if (boot_id == BROM_BOOTSOURCE_USB)
5647a9f8fcSAndy Yan writel(0, CONFIG_ROCKCHIP_BOOT_MODE_REG);
57*ca0ddd93SKever Yang else if (boot_mode == BOOT_BROM_DOWNLOAD)
5847a9f8fcSAndy Yan return true;
5947a9f8fcSAndy Yan }
6047a9f8fcSAndy Yan
6147a9f8fcSAndy Yan return false;
623513fb1eSPhilipp Tomsich }
633513fb1eSPhilipp Tomsich
643513fb1eSPhilipp Tomsich /*
653513fb1eSPhilipp Tomsich * All Rockchip BROM implementations enter with a valid stack-pointer,
663513fb1eSPhilipp Tomsich * so this can safely be implemented in C (providing a single
673513fb1eSPhilipp Tomsich * implementation both for ARMv7 and AArch64).
683513fb1eSPhilipp Tomsich */
save_boot_params(void)693513fb1eSPhilipp Tomsich int save_boot_params(void)
703513fb1eSPhilipp Tomsich {
713513fb1eSPhilipp Tomsich int ret = setjmp(brom_ctx);
723513fb1eSPhilipp Tomsich
733513fb1eSPhilipp Tomsich switch (ret) {
743513fb1eSPhilipp Tomsich case 0:
7547a9f8fcSAndy Yan if (check_back_to_brom_dnl_flag())
7647a9f8fcSAndy Yan _back_to_bootrom(BROM_BOOT_ENTER_DNL);
773513fb1eSPhilipp Tomsich /*
783513fb1eSPhilipp Tomsich * This is the initial pass through this function
793513fb1eSPhilipp Tomsich * (i.e. saving the context), setjmp just setup up the
803513fb1eSPhilipp Tomsich * brom_ctx: transfer back into the startup-code at
813513fb1eSPhilipp Tomsich * 'save_boot_params_ret' and let the compiler know
823513fb1eSPhilipp Tomsich * that this will not return.
833513fb1eSPhilipp Tomsich */
843513fb1eSPhilipp Tomsich save_boot_params_ret();
853513fb1eSPhilipp Tomsich while (true)
863513fb1eSPhilipp Tomsich /* does not return */;
873513fb1eSPhilipp Tomsich break;
883513fb1eSPhilipp Tomsich
893513fb1eSPhilipp Tomsich case BROM_BOOT_NEXTSTAGE:
903513fb1eSPhilipp Tomsich /*
913513fb1eSPhilipp Tomsich * To instruct the BROM to boot the next stage, we
923513fb1eSPhilipp Tomsich * need to return 0 to it: i.e. we need to rewrite
933513fb1eSPhilipp Tomsich * the return code once more.
943513fb1eSPhilipp Tomsich */
953513fb1eSPhilipp Tomsich ret = 0;
963513fb1eSPhilipp Tomsich break;
9747a9f8fcSAndy Yan case BROM_BOOT_ENTER_DNL:
9847a9f8fcSAndy Yan /*
9947a9f8fcSAndy Yan * A non-zero return value will instruct the BROM enter
10047a9f8fcSAndy Yan * download mode.
10147a9f8fcSAndy Yan */
10247a9f8fcSAndy Yan ret = 1;
10347a9f8fcSAndy Yan break;
1043513fb1eSPhilipp Tomsich default:
1053513fb1eSPhilipp Tomsich #if CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT)
1063513fb1eSPhilipp Tomsich puts("FATAL: unexpected command to back_to_bootrom()\n");
1073513fb1eSPhilipp Tomsich #endif
1083513fb1eSPhilipp Tomsich hang();
1093513fb1eSPhilipp Tomsich };
1103513fb1eSPhilipp Tomsich
1113513fb1eSPhilipp Tomsich return ret;
112e1bc64eeSSimon Glass }
113