blob: f7d129be4c8cb3835dd1fc15f9fd206ec562b3f1 [file] [log] [blame]
Hai Phamff3812c2023-02-28 22:37:08 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
Marek Vasut31c81e02025-01-29 18:04:26 +01003 * board/renesas/common/gen4-common.c
Hai Phamff3812c2023-02-28 22:37:08 +01004 *
Marek Vasut10a37c82024-12-12 14:37:34 +01005 * Copyright (C) 2021-2024 Renesas Electronics Corp.
Hai Phamff3812c2023-02-28 22:37:08 +01006 */
7
Marek Vasut97a070b2024-02-27 17:05:54 +01008#include <asm/arch/renesas.h>
Hai Phamff3812c2023-02-28 22:37:08 +01009#include <asm/arch/sys_proto.h>
Marek Vasut75183ab2025-03-02 21:59:11 +010010#include <asm/armv8/mmu.h>
Hai Phamff3812c2023-02-28 22:37:08 +010011#include <asm/global_data.h>
12#include <asm/io.h>
13#include <asm/mach-types.h>
14#include <asm/processor.h>
Hai Phamff3812c2023-02-28 22:37:08 +010015#include <asm/system.h>
Marek Vasut75183ab2025-03-02 21:59:11 +010016#include <image.h>
Marek Vasut10a37c82024-12-12 14:37:34 +010017#include <linux/errno.h>
18
19#define RST_BASE 0xE6160000 /* Domain0 */
20#define RST_WDTRSTCR (RST_BASE + 0x10)
21#define RST_RWDT 0xA55A8002
Hai Phamff3812c2023-02-28 22:37:08 +010022
23DECLARE_GLOBAL_DATA_PTR;
24
25static void init_generic_timer(void)
26{
27 const u32 freq = CONFIG_SYS_CLK_FREQ;
28
29 /* Update memory mapped and register based freqency */
30 asm volatile ("msr cntfrq_el0, %0" :: "r" (freq));
31 writel(freq, CNTFID0);
32
33 /* Enable counter */
34 setbits_le32(CNTCR_BASE, CNTCR_EN);
35}
36
37static void init_gic_v3(void)
38{
Marek Vasut00eed1e2024-01-21 18:33:12 +010039 /* GIC v3 power on */
Hai Phamff3812c2023-02-28 22:37:08 +010040 writel(BIT(1), GICR_LPI_PWRR);
41
42 /* Wait till the WAKER_CA_BIT changes to 0 */
43 clrbits_le32(GICR_LPI_WAKER, BIT(1));
44 while (readl(GICR_LPI_WAKER) & BIT(2))
45 ;
46
47 writel(0xffffffff, GICR_SGI_BASE + GICR_IGROUPR0);
48}
49
50void s_init(void)
51{
52 if (current_el() == 3)
53 init_generic_timer();
54}
55
56int board_early_init_f(void)
57{
58 /* Unlock CPG access */
59 writel(0x5A5AFFFF, CPGWPR);
60 writel(0xA5A50000, CPGWPCR);
61
62 return 0;
63}
64
65int board_init(void)
66{
Marek Vasut10a37c82024-12-12 14:37:34 +010067 if (current_el() != 3)
68 return 0;
69 init_gic_v3();
70
71 /* Enable RWDT reset on V3U in EL3 */
72 if (IS_ENABLED(CONFIG_R8A779A0) &&
73 renesas_get_cpu_type() == RENESAS_CPU_TYPE_R8A779A0) {
74 writel(RST_RWDT, RST_WDTRSTCR);
75 }
Hai Phamff3812c2023-02-28 22:37:08 +010076
77 return 0;
78}
Marek Vasutf48b1332024-12-12 14:38:27 +010079
80#define RST_BASE 0xE6160000 /* Domain0 */
81#define RST_SRESCR0 (RST_BASE + 0x18)
82#define RST_SPRES 0x5AA58000
83
84void __weak reset_cpu(void)
85{
86 writel(RST_SPRES, RST_SRESCR0);
87}
88
89int ft_board_setup(void *blob, struct bd_info *bd)
90{
91 return 0;
92}
Marek Vasut75183ab2025-03-02 21:59:11 +010093
94/* R-Car Gen4 TFA BL31 handoff structure and handling. */
95struct param_header {
96 u8 type;
97 u8 version;
98 u16 size;
99 u32 attr;
100};
101
102struct tfa_image_info {
103 struct param_header h;
104 uintptr_t image_base;
105 u32 image_size;
106 u32 image_max_size;
107};
108
109struct aapcs64_params {
110 u64 arg0;
111 u64 arg1;
112 u64 arg2;
113 u64 arg3;
114 u64 arg4;
115 u64 arg5;
116 u64 arg6;
117 u64 arg7;
118};
119
120struct entry_point_info {
121 struct param_header h;
122 uintptr_t pc;
123 u32 spsr;
124 struct aapcs64_params args;
125};
126
127struct bl2_to_bl31_params_mem {
128 struct tfa_image_info bl32_image_info;
129 struct tfa_image_info bl33_image_info;
130 struct entry_point_info bl33_ep_info;
131 struct entry_point_info bl32_ep_info;
132};
133
134/* Default jump address, return to U-Boot */
135#define BL33_BASE 0x44100000
136/* Custom parameters address passed to TFA by ICUMXA loader */
137#define PARAMS_BASE 0x46422200
138
139/* Usually such a structure is produced by ICUMXA and passed in at 0x46422200 */
140static const struct bl2_to_bl31_params_mem blinfo_template = {
141 .bl33_ep_info.h.type = 1, /* PARAM_EP */
142 .bl33_ep_info.h.version = 2, /* Version 2 */
143 .bl33_ep_info.h.size = sizeof(struct entry_point_info),
144 .bl33_ep_info.h.attr = 0x81, /* Executable | Non-Secure */
145 .bl33_ep_info.spsr = 0x2c9, /* Mode=EL2, SP=ELX, Exceptions=OFF */
146 .bl33_ep_info.pc = BL33_BASE,
147
148 .bl33_image_info.h.type = 1, /* PARAM_EP */
149 .bl33_image_info.h.version = 2, /* Version 2 */
150 .bl33_image_info.h.size = sizeof(struct image_info),
151 .bl33_image_info.h.attr = 0,
152 .bl33_image_info.image_base = BL33_BASE,
153};
154
155static bool tfa_bl31_image_loaded;
156static ulong tfa_bl31_image_addr;
157
158static void tfa_bl31_image_process(ulong image, size_t size)
159{
160 /* Custom parameters address passed to TFA by ICUMXA loader */
161 struct bl2_to_bl31_params_mem *blinfo = (struct bl2_to_bl31_params_mem *)PARAMS_BASE;
162
163 /* Not in EL3, do nothing. */
164 if (current_el() != 3)
165 return;
166
167 /* Clear a page and copy template */
168 memset((void *)PARAMS_BASE, 0, PAGE_SIZE);
169 memcpy(blinfo, &blinfo_template, sizeof(*blinfo));
170 tfa_bl31_image_addr = image;
171 tfa_bl31_image_loaded = true;
172}
173
174U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_TFA_BL31, tfa_bl31_image_process);
175
176void armv8_switch_to_el2_prep(u64 args, u64 mach_nr, u64 fdt_addr,
177 u64 arg4, u64 entry_point, u64 es_flag)
178{
179 typedef void __noreturn (*image_entry_noargs_t)(void);
180 image_entry_noargs_t image_entry =
181 (image_entry_noargs_t)(void *)tfa_bl31_image_addr;
182 struct bl2_to_bl31_params_mem *blinfo =
183 (struct bl2_to_bl31_params_mem *)PARAMS_BASE;
184
185 /* Not in EL3, do nothing. */
186 if (current_el() != 3)
187 return;
188
189 /*
190 * Destination address in arch/arm/cpu/armv8/transition.S
191 * right past the first bl in armv8_switch_to_el2() to let
192 * the rest of U-Boot pre-Linux code run. The code does run
193 * without stack pointer!
194 */
195 const u64 ep = ((u64)(uintptr_t)&armv8_switch_to_el2) + 4;
196
197 /* If TFA BL31 was not part of the fitImage, do regular boot. */
198 if (!tfa_bl31_image_loaded)
199 return;
200
201 /*
202 * Set up kernel entry point and parameters:
203 * x0 is FDT address, x1..x3 must be 0
204 */
205 blinfo->bl33_ep_info.pc = ep;
206 blinfo->bl33_ep_info.args.arg0 = args;
207 blinfo->bl33_ep_info.args.arg1 = mach_nr;
208 blinfo->bl33_ep_info.args.arg2 = fdt_addr;
209 blinfo->bl33_ep_info.args.arg3 = arg4;
210 blinfo->bl33_ep_info.args.arg4 = entry_point;
211 blinfo->bl33_ep_info.args.arg5 = es_flag;
212 blinfo->bl33_image_info.image_base = ep;
213
214 /* Jump to TFA BL31 */
215 image_entry();
216}