blob: 2c663291232d40e59bc350410a3465cecab79ab4 [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
7#include <arch.h>
8#include <assert.h>
9#include <console.h>
10#include <debug.h>
11#include <generic_delay_timer.h>
12#include <gicv2.h>
Andre Przywaraea5fa472018-09-16 02:08:06 +010013#include <libfdt.h>
Andre Przywarae1eb4362018-11-04 23:37:48 +000014#include <mmio.h>
Samuel Hollandb8566642017-08-12 04:07:39 -050015#include <platform.h>
16#include <platform_def.h>
17#include <sunxi_def.h>
18#include <sunxi_mmap.h>
Andre Przywara456208a2018-10-14 12:02:02 +010019#include <sunxi_private.h>
Samuel Hollandb8566642017-08-12 04:07:39 -050020#include <uart_16550.h>
21
Samuel Hollandb8566642017-08-12 04:07:39 -050022
Amit Singh Tomar2f372242018-06-20 00:44:50 +053023static entry_point_info_t bl32_image_ep_info;
Samuel Hollandb8566642017-08-12 04:07:39 -050024static entry_point_info_t bl33_image_ep_info;
25
26static console_16550_t console;
27
28static const gicv2_driver_data_t sunxi_gic_data = {
29 .gicd_base = SUNXI_GICD_BASE,
30 .gicc_base = SUNXI_GICC_BASE,
31};
32
Andre Przywaraea5fa472018-09-16 02:08:06 +010033/*
34 * Try to find a DTB loaded in memory by previous stages.
35 *
36 * At the moment we implement a heuristic to find the DTB attached to U-Boot:
37 * U-Boot appends its DTB to the end of the image. Assuming that BL33 is
38 * U-Boot, try to find the size of the U-Boot image to learn the DTB address.
39 * The generic ARMv8 U-Boot image contains the load address and its size
40 * as u64 variables at the beginning of the image. There might be padding
41 * or other headers before that data, so scan the first 2KB after the BL33
42 * entry point to find the load address, which should be followed by the
43 * size. Adding those together gives us the address of the DTB.
44 */
45static void *sunxi_find_dtb(void)
46{
47 uint64_t *u_boot_base;
48 int i;
49
50 u_boot_base = (void *)(SUNXI_DRAM_VIRT_BASE + SUNXI_DRAM_SEC_SIZE);
51
52 for (i = 0; i < 2048 / sizeof(uint64_t); i++) {
53 uint32_t *dtb_base;
54
55 if (u_boot_base[i] != PLAT_SUNXI_NS_IMAGE_OFFSET)
56 continue;
57
58 /* Does the suspected U-Boot size look anyhow reasonable? */
59 if (u_boot_base[i + 1] >= 256 * 1024 * 1024)
60 continue;
61
62 /* end of the image: base address + size */
63 dtb_base = (void *)((char *)u_boot_base + u_boot_base[i + 1]);
64
65 if (fdt_check_header(dtb_base) != 0)
66 continue;
67
68 return dtb_base;
69 }
70
71 return NULL;
72}
73
Samuel Hollandb8566642017-08-12 04:07:39 -050074void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
75 u_register_t arg2, u_register_t arg3)
76{
77 /* Initialize the debug console as soon as possible */
78 console_16550_register(SUNXI_UART0_BASE, SUNXI_UART0_CLK_IN_HZ,
79 SUNXI_UART0_BAUDRATE, &console);
80
Amit Singh Tomar2f372242018-06-20 00:44:50 +053081#ifdef BL32_BASE
82 /* Populate entry point information for BL32 */
83 SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
84 SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
85 bl32_image_ep_info.pc = BL32_BASE;
86#endif
87
Samuel Hollandb8566642017-08-12 04:07:39 -050088 /* Populate entry point information for BL33 */
89 SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
90 /*
91 * Tell BL31 where the non-trusted software image
92 * is located and the entry state information
93 */
94 bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
95 bl33_image_ep_info.spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
96 DISABLE_ALL_EXCEPTIONS);
97 SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
Samuel Holland321c0ab2017-08-12 04:07:39 -050098
99 /* Turn off all secondary CPUs */
100 sunxi_disable_secondary_cpus(plat_my_core_pos());
Samuel Hollandb8566642017-08-12 04:07:39 -0500101}
102
103void bl31_plat_arch_setup(void)
104{
105 sunxi_configure_mmu_el3(0);
106}
107
108void bl31_platform_setup(void)
109{
Andre Przywarac2366b92018-06-22 00:47:08 +0100110 const char *soc_name;
111 uint16_t soc_id = sunxi_read_soc_id();
Andre Przywaraea5fa472018-09-16 02:08:06 +0100112 void *fdt;
Andre Przywarac2366b92018-06-22 00:47:08 +0100113
114 switch (soc_id) {
Andre Przywara78dca1f2018-09-17 00:03:09 +0100115 case SUNXI_SOC_A64:
Andre Przywarac2366b92018-06-22 00:47:08 +0100116 soc_name = "A64/H64/R18";
117 break;
Andre Przywara78dca1f2018-09-17 00:03:09 +0100118 case SUNXI_SOC_H5:
Andre Przywarac2366b92018-06-22 00:47:08 +0100119 soc_name = "H5";
120 break;
Andre Przywara78dca1f2018-09-17 00:03:09 +0100121 case SUNXI_SOC_H6:
Andre Przywaraaa26f532017-12-08 01:27:02 +0000122 soc_name = "H6";
123 break;
Andre Przywarac2366b92018-06-22 00:47:08 +0100124 default:
125 soc_name = "unknown";
126 break;
127 }
128 NOTICE("BL31: Detected Allwinner %s SoC (%04x)\n", soc_name, soc_id);
129
Samuel Hollandb8566642017-08-12 04:07:39 -0500130 generic_delay_timer_init();
131
Andre Przywaraea5fa472018-09-16 02:08:06 +0100132 fdt = sunxi_find_dtb();
133 if (fdt) {
134 const char *model;
135 int length;
136
137 model = fdt_getprop(fdt, 0, "model", &length);
138 NOTICE("BL31: Found U-Boot DTB at %p, model: %s\n", fdt,
139 model ?: "unknown");
140 } else {
141 NOTICE("BL31: No DTB found.\n");
142 }
143
Samuel Hollandb8566642017-08-12 04:07:39 -0500144 /* Configure the interrupt controller */
145 gicv2_driver_init(&sunxi_gic_data);
146 gicv2_distif_init();
147 gicv2_pcpu_distif_init();
148 gicv2_cpuif_enable();
149
Andre Przywara13815472018-06-01 02:01:39 +0100150 sunxi_security_setup();
151
Andre Przywarae1eb4362018-11-04 23:37:48 +0000152 /*
153 * On the A64 U-Boot's SPL sets the bus clocks to some conservative
154 * values, to work around FEL mode instabilities with SRAM C accesses.
155 * FEL mode is gone when we reach ATF, so bring the AHB1 bus
156 * (the "main" bus) clock frequency back to the recommended 200MHz,
157 * for improved performance.
158 */
159 if (soc_id == SUNXI_SOC_A64)
160 mmio_write_32(SUNXI_CCU_BASE + 0x54, 0x00003180);
161
162 /*
163 * U-Boot or the kernel don't setup AHB2, which leaves it at the
164 * AHB1 frequency (200 MHz, see above). However Allwinner recommends
165 * 300 MHz, for improved Ethernet and USB performance. Switch the
166 * clock to use "PLL_PERIPH0 / 2".
167 */
168 if (soc_id == SUNXI_SOC_A64 || soc_id == SUNXI_SOC_H5)
169 mmio_write_32(SUNXI_CCU_BASE + 0x5c, 0x1);
170
Andre Przywara4e4b1e62018-09-08 19:18:37 +0100171 sunxi_pmic_setup(soc_id, fdt);
Icenowy Zheng7508bef2018-07-21 20:41:12 +0800172
Samuel Hollandb8566642017-08-12 04:07:39 -0500173 INFO("BL31: Platform setup done\n");
174}
175
176entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
177{
178 assert(sec_state_is_valid(type) != 0);
Amit Singh Tomar2f372242018-06-20 00:44:50 +0530179
180 if (type == NON_SECURE)
181 return &bl33_image_ep_info;
182
183 if ((type == SECURE) && bl32_image_ep_info.pc)
184 return &bl32_image_ep_info;
Samuel Hollandb8566642017-08-12 04:07:39 -0500185
Amit Singh Tomar2f372242018-06-20 00:44:50 +0530186 return NULL;
Samuel Hollandb8566642017-08-12 04:07:39 -0500187}