blob: b619b18edaba80689049832bb9ff8cbaf3829f71 [file] [log] [blame]
Samuel Hollandb8566642017-08-12 04:07:39 -05001/*
2 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
3 *
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>
16#include <drivers/arm/gicv2.h>
17#include <drivers/console.h>
18#include <drivers/generic_delay_timer.h>
19#include <drivers/ti/uart/uart_16550.h>
20#include <lib/mmio.h>
21#include <plat/common/platform.h>
22
Samuel Hollandb8566642017-08-12 04:07:39 -050023#include <sunxi_def.h>
24#include <sunxi_mmap.h>
Andre Przywara456208a2018-10-14 12:02:02 +010025#include <sunxi_private.h>
Samuel Hollandb8566642017-08-12 04:07:39 -050026
Samuel Hollandb8566642017-08-12 04:07:39 -050027
Amit Singh Tomar2f372242018-06-20 00:44:50 +053028static entry_point_info_t bl32_image_ep_info;
Samuel Hollandb8566642017-08-12 04:07:39 -050029static entry_point_info_t bl33_image_ep_info;
30
Andre Przywara98b5a112020-01-25 00:58:35 +000031static console_t console;
Samuel Hollandb8566642017-08-12 04:07:39 -050032
33static const gicv2_driver_data_t sunxi_gic_data = {
34 .gicd_base = SUNXI_GICD_BASE,
35 .gicc_base = SUNXI_GICC_BASE,
36};
37
Andre Przywaraea5fa472018-09-16 02:08:06 +010038/*
39 * Try to find a DTB loaded in memory by previous stages.
40 *
41 * At the moment we implement a heuristic to find the DTB attached to U-Boot:
42 * U-Boot appends its DTB to the end of the image. Assuming that BL33 is
43 * U-Boot, try to find the size of the U-Boot image to learn the DTB address.
44 * The generic ARMv8 U-Boot image contains the load address and its size
45 * as u64 variables at the beginning of the image. There might be padding
46 * or other headers before that data, so scan the first 2KB after the BL33
47 * entry point to find the load address, which should be followed by the
48 * size. Adding those together gives us the address of the DTB.
49 */
50static void *sunxi_find_dtb(void)
51{
52 uint64_t *u_boot_base;
53 int i;
54
55 u_boot_base = (void *)(SUNXI_DRAM_VIRT_BASE + SUNXI_DRAM_SEC_SIZE);
56
57 for (i = 0; i < 2048 / sizeof(uint64_t); i++) {
58 uint32_t *dtb_base;
59
Samuel Hollandafe21732020-12-13 20:05:11 -060060 if (u_boot_base[i] != PRELOADED_BL33_BASE)
Andre Przywaraea5fa472018-09-16 02:08:06 +010061 continue;
62
63 /* Does the suspected U-Boot size look anyhow reasonable? */
64 if (u_boot_base[i + 1] >= 256 * 1024 * 1024)
65 continue;
66
67 /* end of the image: base address + size */
68 dtb_base = (void *)((char *)u_boot_base + u_boot_base[i + 1]);
69
70 if (fdt_check_header(dtb_base) != 0)
71 continue;
72
73 return dtb_base;
74 }
75
76 return NULL;
77}
78
Samuel Hollandb8566642017-08-12 04:07:39 -050079void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
80 u_register_t arg2, u_register_t arg3)
81{
82 /* Initialize the debug console as soon as possible */
83 console_16550_register(SUNXI_UART0_BASE, SUNXI_UART0_CLK_IN_HZ,
84 SUNXI_UART0_BAUDRATE, &console);
85
Amit Singh Tomar2f372242018-06-20 00:44:50 +053086#ifdef BL32_BASE
87 /* Populate entry point information for BL32 */
88 SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
89 SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
90 bl32_image_ep_info.pc = BL32_BASE;
91#endif
92
Samuel Hollandb8566642017-08-12 04:07:39 -050093 /* Populate entry point information for BL33 */
94 SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
95 /*
96 * Tell BL31 where the non-trusted software image
97 * is located and the entry state information
98 */
Samuel Hollandafe21732020-12-13 20:05:11 -060099 bl33_image_ep_info.pc = PRELOADED_BL33_BASE;
Samuel Hollandb8566642017-08-12 04:07:39 -0500100 bl33_image_ep_info.spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
101 DISABLE_ALL_EXCEPTIONS);
102 SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
Samuel Hollandb8566642017-08-12 04:07:39 -0500103}
104
105void bl31_plat_arch_setup(void)
106{
107 sunxi_configure_mmu_el3(0);
108}
109
110void bl31_platform_setup(void)
111{
Andre Przywarac2366b92018-06-22 00:47:08 +0100112 const char *soc_name;
113 uint16_t soc_id = sunxi_read_soc_id();
Andre Przywaraea5fa472018-09-16 02:08:06 +0100114 void *fdt;
Andre Przywarac2366b92018-06-22 00:47:08 +0100115
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 Przywarac2366b92018-06-22 00:47:08 +0100126 default:
127 soc_name = "unknown";
128 break;
129 }
130 NOTICE("BL31: Detected Allwinner %s SoC (%04x)\n", soc_name, soc_id);
131
Samuel Hollandb8566642017-08-12 04:07:39 -0500132 generic_delay_timer_init();
133
Andre Przywaraea5fa472018-09-16 02:08:06 +0100134 fdt = sunxi_find_dtb();
135 if (fdt) {
136 const char *model;
137 int length;
138
139 model = fdt_getprop(fdt, 0, "model", &length);
140 NOTICE("BL31: Found U-Boot DTB at %p, model: %s\n", fdt,
141 model ?: "unknown");
142 } else {
143 NOTICE("BL31: No DTB found.\n");
144 }
145
Samuel Hollandb8566642017-08-12 04:07:39 -0500146 /* Configure the interrupt controller */
147 gicv2_driver_init(&sunxi_gic_data);
148 gicv2_distif_init();
149 gicv2_pcpu_distif_init();
150 gicv2_cpuif_enable();
151
Andre Przywara13815472018-06-01 02:01:39 +0100152 sunxi_security_setup();
153
Andre Przywarae1eb4362018-11-04 23:37:48 +0000154 /*
155 * On the A64 U-Boot's SPL sets the bus clocks to some conservative
156 * values, to work around FEL mode instabilities with SRAM C accesses.
157 * FEL mode is gone when we reach ATF, so bring the AHB1 bus
158 * (the "main" bus) clock frequency back to the recommended 200MHz,
159 * for improved performance.
160 */
161 if (soc_id == SUNXI_SOC_A64)
162 mmio_write_32(SUNXI_CCU_BASE + 0x54, 0x00003180);
163
164 /*
165 * U-Boot or the kernel don't setup AHB2, which leaves it at the
166 * AHB1 frequency (200 MHz, see above). However Allwinner recommends
167 * 300 MHz, for improved Ethernet and USB performance. Switch the
168 * clock to use "PLL_PERIPH0 / 2".
169 */
170 if (soc_id == SUNXI_SOC_A64 || soc_id == SUNXI_SOC_H5)
171 mmio_write_32(SUNXI_CCU_BASE + 0x5c, 0x1);
172
Andre Przywara4e4b1e62018-09-08 19:18:37 +0100173 sunxi_pmic_setup(soc_id, fdt);
Icenowy Zheng7508bef2018-07-21 20:41:12 +0800174
Samuel Hollandb8566642017-08-12 04:07:39 -0500175 INFO("BL31: Platform setup done\n");
176}
177
178entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
179{
180 assert(sec_state_is_valid(type) != 0);
Amit Singh Tomar2f372242018-06-20 00:44:50 +0530181
182 if (type == NON_SECURE)
183 return &bl33_image_ep_info;
184
185 if ((type == SECURE) && bl32_image_ep_info.pc)
186 return &bl32_image_ep_info;
Samuel Hollandb8566642017-08-12 04:07:39 -0500187
Amit Singh Tomar2f372242018-06-20 00:44:50 +0530188 return NULL;
Samuel Hollandb8566642017-08-12 04:07:39 -0500189}