blob: 263083bb173a264c6c5900460685414b72d254c6 [file] [log] [blame]
Samuel Hollandb8566642017-08-12 04:07:39 -05001/*
Salman Nabifc12f8d2024-02-19 13:42:56 +00002 * Copyright (c) 2017-2024, Arm Limited and Contributors. All rights reserved.
Samuel Hollandb8566642017-08-12 04:07:39 -05003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Samuel Hollandb8566642017-08-12 04:07:39 -05007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
Andre Przywaraea5fa472018-09-16 02:08:06 +01009#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
Samuel Hollandb8566642017-08-12 04:07:39 -050011#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
13#include <arch.h>
Samuel Hollandc629daf2019-02-17 15:33:33 -060014#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <common/debug.h>
Andre Przywarafb838332020-12-14 12:06:24 +000016#include <common/fdt_fixup.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <drivers/arm/gicv2.h>
18#include <drivers/console.h>
19#include <drivers/generic_delay_timer.h>
20#include <drivers/ti/uart/uart_16550.h>
21#include <lib/mmio.h>
22#include <plat/common/platform.h>
23
Samuel Hollandb8566642017-08-12 04:07:39 -050024#include <sunxi_def.h>
25#include <sunxi_mmap.h>
Andre Przywara456208a2018-10-14 12:02:02 +010026#include <sunxi_private.h>
Samuel Hollandb8566642017-08-12 04:07:39 -050027
Samuel Hollandb8566642017-08-12 04:07:39 -050028
Amit Singh Tomar2f372242018-06-20 00:44:50 +053029static entry_point_info_t bl32_image_ep_info;
Samuel Hollandb8566642017-08-12 04:07:39 -050030static entry_point_info_t bl33_image_ep_info;
31
Andre Przywara98b5a112020-01-25 00:58:35 +000032static console_t console;
Samuel Hollandb8566642017-08-12 04:07:39 -050033
Samuel Hollandf96cb0a2022-01-22 22:06:57 -060034static void *fdt;
35
Samuel Hollandb8566642017-08-12 04:07:39 -050036static const gicv2_driver_data_t sunxi_gic_data = {
37 .gicd_base = SUNXI_GICD_BASE,
38 .gicc_base = SUNXI_GICC_BASE,
39};
40
Andre Przywaraea5fa472018-09-16 02:08:06 +010041/*
42 * Try to find a DTB loaded in memory by previous stages.
43 *
44 * At the moment we implement a heuristic to find the DTB attached to U-Boot:
45 * U-Boot appends its DTB to the end of the image. Assuming that BL33 is
46 * U-Boot, try to find the size of the U-Boot image to learn the DTB address.
47 * The generic ARMv8 U-Boot image contains the load address and its size
48 * as u64 variables at the beginning of the image. There might be padding
49 * or other headers before that data, so scan the first 2KB after the BL33
50 * entry point to find the load address, which should be followed by the
51 * size. Adding those together gives us the address of the DTB.
52 */
Samuel Hollandf96cb0a2022-01-22 22:06:57 -060053static void sunxi_find_dtb(void)
Andre Przywaraea5fa472018-09-16 02:08:06 +010054{
55 uint64_t *u_boot_base;
56 int i;
57
Andre Przywaracd1c67e2020-11-28 01:38:15 +000058 u_boot_base = (void *)SUNXI_BL33_VIRT_BASE;
Andre Przywaraea5fa472018-09-16 02:08:06 +010059
60 for (i = 0; i < 2048 / sizeof(uint64_t); i++) {
Samuel Hollandf96cb0a2022-01-22 22:06:57 -060061 void *dtb_base;
Andre Przywaraea5fa472018-09-16 02:08:06 +010062
Samuel Hollandafe21732020-12-13 20:05:11 -060063 if (u_boot_base[i] != PRELOADED_BL33_BASE)
Andre Przywaraea5fa472018-09-16 02:08:06 +010064 continue;
65
66 /* Does the suspected U-Boot size look anyhow reasonable? */
67 if (u_boot_base[i + 1] >= 256 * 1024 * 1024)
68 continue;
69
70 /* end of the image: base address + size */
Samuel Hollandf96cb0a2022-01-22 22:06:57 -060071 dtb_base = (char *)u_boot_base + u_boot_base[i + 1];
Andre Przywaraea5fa472018-09-16 02:08:06 +010072
Samuel Hollandf96cb0a2022-01-22 22:06:57 -060073 if (fdt_check_header(dtb_base) == 0) {
74 fdt = dtb_base;
75 return;
76 }
Andre Przywaraea5fa472018-09-16 02:08:06 +010077 }
Andre Przywaraea5fa472018-09-16 02:08:06 +010078}
79
Samuel Hollandb8566642017-08-12 04:07:39 -050080void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
81 u_register_t arg2, u_register_t arg3)
82{
83 /* Initialize the debug console as soon as possible */
84 console_16550_register(SUNXI_UART0_BASE, SUNXI_UART0_CLK_IN_HZ,
85 SUNXI_UART0_BAUDRATE, &console);
86
Amit Singh Tomar2f372242018-06-20 00:44:50 +053087#ifdef BL32_BASE
88 /* Populate entry point information for BL32 */
89 SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
90 SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
91 bl32_image_ep_info.pc = BL32_BASE;
92#endif
93
Samuel Hollandb8566642017-08-12 04:07:39 -050094 /* Populate entry point information for BL33 */
95 SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
96 /*
97 * Tell BL31 where the non-trusted software image
98 * is located and the entry state information
99 */
Samuel Hollandafe21732020-12-13 20:05:11 -0600100 bl33_image_ep_info.pc = PRELOADED_BL33_BASE;
Samuel Hollandb8566642017-08-12 04:07:39 -0500101 bl33_image_ep_info.spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
102 DISABLE_ALL_EXCEPTIONS);
103 SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
Samuel Hollandb8566642017-08-12 04:07:39 -0500104}
105
106void bl31_plat_arch_setup(void)
107{
108 sunxi_configure_mmu_el3(0);
109}
110
111void bl31_platform_setup(void)
112{
Andre Przywarac2366b92018-06-22 00:47:08 +0100113 const char *soc_name;
114 uint16_t soc_id = sunxi_read_soc_id();
115
116 switch (soc_id) {
Andre Przywara78dca1f2018-09-17 00:03:09 +0100117 case SUNXI_SOC_A64:
Andre Przywarac2366b92018-06-22 00:47:08 +0100118 soc_name = "A64/H64/R18";
119 break;
Andre Przywara78dca1f2018-09-17 00:03:09 +0100120 case SUNXI_SOC_H5:
Andre Przywarac2366b92018-06-22 00:47:08 +0100121 soc_name = "H5";
122 break;
Andre Przywara78dca1f2018-09-17 00:03:09 +0100123 case SUNXI_SOC_H6:
Andre Przywaraaa26f532017-12-08 01:27:02 +0000124 soc_name = "H6";
125 break;
Andre Przywarabafb5612020-11-24 11:07:10 +0000126 case SUNXI_SOC_H616:
127 soc_name = "H616";
128 break;
Icenowy Zheng61da7562021-07-22 09:41:16 +0800129 case SUNXI_SOC_R329:
130 soc_name = "R329";
131 break;
Andre Przywarac2366b92018-06-22 00:47:08 +0100132 default:
133 soc_name = "unknown";
134 break;
135 }
136 NOTICE("BL31: Detected Allwinner %s SoC (%04x)\n", soc_name, soc_id);
137
Samuel Hollandb8566642017-08-12 04:07:39 -0500138 generic_delay_timer_init();
139
Samuel Hollandf96cb0a2022-01-22 22:06:57 -0600140 sunxi_find_dtb();
Andre Przywaraea5fa472018-09-16 02:08:06 +0100141 if (fdt) {
142 const char *model;
143 int length;
144
145 model = fdt_getprop(fdt, 0, "model", &length);
146 NOTICE("BL31: Found U-Boot DTB at %p, model: %s\n", fdt,
147 model ?: "unknown");
148 } else {
149 NOTICE("BL31: No DTB found.\n");
150 }
151
Samuel Hollandb8566642017-08-12 04:07:39 -0500152 /* Configure the interrupt controller */
153 gicv2_driver_init(&sunxi_gic_data);
154 gicv2_distif_init();
155 gicv2_pcpu_distif_init();
156 gicv2_cpuif_enable();
157
Andre Przywara13815472018-06-01 02:01:39 +0100158 sunxi_security_setup();
159
Andre Przywarae1eb4362018-11-04 23:37:48 +0000160 /*
161 * On the A64 U-Boot's SPL sets the bus clocks to some conservative
162 * values, to work around FEL mode instabilities with SRAM C accesses.
163 * FEL mode is gone when we reach ATF, so bring the AHB1 bus
164 * (the "main" bus) clock frequency back to the recommended 200MHz,
165 * for improved performance.
166 */
167 if (soc_id == SUNXI_SOC_A64)
168 mmio_write_32(SUNXI_CCU_BASE + 0x54, 0x00003180);
169
170 /*
171 * U-Boot or the kernel don't setup AHB2, which leaves it at the
172 * AHB1 frequency (200 MHz, see above). However Allwinner recommends
173 * 300 MHz, for improved Ethernet and USB performance. Switch the
174 * clock to use "PLL_PERIPH0 / 2".
175 */
176 if (soc_id == SUNXI_SOC_A64 || soc_id == SUNXI_SOC_H5)
177 mmio_write_32(SUNXI_CCU_BASE + 0x5c, 0x1);
178
Andre Przywara4e4b1e62018-09-08 19:18:37 +0100179 sunxi_pmic_setup(soc_id, fdt);
Icenowy Zheng7508bef2018-07-21 20:41:12 +0800180
Samuel Hollandf96cb0a2022-01-22 22:06:57 -0600181 INFO("BL31: Platform setup done\n");
182}
183
184void bl31_plat_runtime_setup(void)
185{
Andre Przywara9de12222021-12-19 13:39:40 +0000186 /* Change the DTB if the configuration requires so. */
Andre Przywarafb838332020-12-14 12:06:24 +0000187 sunxi_prepare_dtb(fdt);
Samuel Hollandb8566642017-08-12 04:07:39 -0500188}
189
190entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
191{
192 assert(sec_state_is_valid(type) != 0);
Amit Singh Tomar2f372242018-06-20 00:44:50 +0530193
194 if (type == NON_SECURE)
195 return &bl33_image_ep_info;
196
197 if ((type == SECURE) && bl32_image_ep_info.pc)
198 return &bl32_image_ep_info;
Samuel Hollandb8566642017-08-12 04:07:39 -0500199
Amit Singh Tomar2f372242018-06-20 00:44:50 +0530200 return NULL;
Samuel Hollandb8566642017-08-12 04:07:39 -0500201}