Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 1 | /* |
Samuel Holland | f96cb0a | 2022-01-22 22:06:57 -0600 | [diff] [blame] | 2 | * Copyright (c) 2017-2022, ARM Limited and Contributors. All rights reserved. |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | |
Andre Przywara | ea5fa47 | 2018-09-16 02:08:06 +0100 | [diff] [blame] | 9 | #include <libfdt.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 11 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <arch.h> |
Samuel Holland | c629daf | 2019-02-17 15:33:33 -0600 | [diff] [blame] | 14 | #include <arch_helpers.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 15 | #include <common/debug.h> |
Andre Przywara | fb83833 | 2020-12-14 12:06:24 +0000 | [diff] [blame] | 16 | #include <common/fdt_fixup.h> |
| 17 | #include <common/fdt_wrappers.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 18 | #include <drivers/arm/gicv2.h> |
| 19 | #include <drivers/console.h> |
| 20 | #include <drivers/generic_delay_timer.h> |
| 21 | #include <drivers/ti/uart/uart_16550.h> |
| 22 | #include <lib/mmio.h> |
| 23 | #include <plat/common/platform.h> |
| 24 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 25 | #include <sunxi_def.h> |
| 26 | #include <sunxi_mmap.h> |
Andre Przywara | 456208a | 2018-10-14 12:02:02 +0100 | [diff] [blame] | 27 | #include <sunxi_private.h> |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 28 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 29 | |
Amit Singh Tomar | 2f37224 | 2018-06-20 00:44:50 +0530 | [diff] [blame] | 30 | static entry_point_info_t bl32_image_ep_info; |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 31 | static entry_point_info_t bl33_image_ep_info; |
| 32 | |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 33 | static console_t console; |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 34 | |
Samuel Holland | f96cb0a | 2022-01-22 22:06:57 -0600 | [diff] [blame] | 35 | static void *fdt; |
| 36 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 37 | static const gicv2_driver_data_t sunxi_gic_data = { |
| 38 | .gicd_base = SUNXI_GICD_BASE, |
| 39 | .gicc_base = SUNXI_GICC_BASE, |
| 40 | }; |
| 41 | |
Andre Przywara | ea5fa47 | 2018-09-16 02:08:06 +0100 | [diff] [blame] | 42 | /* |
| 43 | * Try to find a DTB loaded in memory by previous stages. |
| 44 | * |
| 45 | * At the moment we implement a heuristic to find the DTB attached to U-Boot: |
| 46 | * U-Boot appends its DTB to the end of the image. Assuming that BL33 is |
| 47 | * U-Boot, try to find the size of the U-Boot image to learn the DTB address. |
| 48 | * The generic ARMv8 U-Boot image contains the load address and its size |
| 49 | * as u64 variables at the beginning of the image. There might be padding |
| 50 | * or other headers before that data, so scan the first 2KB after the BL33 |
| 51 | * entry point to find the load address, which should be followed by the |
| 52 | * size. Adding those together gives us the address of the DTB. |
| 53 | */ |
Samuel Holland | f96cb0a | 2022-01-22 22:06:57 -0600 | [diff] [blame] | 54 | static void sunxi_find_dtb(void) |
Andre Przywara | ea5fa47 | 2018-09-16 02:08:06 +0100 | [diff] [blame] | 55 | { |
| 56 | uint64_t *u_boot_base; |
| 57 | int i; |
| 58 | |
Andre Przywara | cd1c67e | 2020-11-28 01:38:15 +0000 | [diff] [blame] | 59 | u_boot_base = (void *)SUNXI_BL33_VIRT_BASE; |
Andre Przywara | ea5fa47 | 2018-09-16 02:08:06 +0100 | [diff] [blame] | 60 | |
| 61 | for (i = 0; i < 2048 / sizeof(uint64_t); i++) { |
Samuel Holland | f96cb0a | 2022-01-22 22:06:57 -0600 | [diff] [blame] | 62 | void *dtb_base; |
Andre Przywara | ea5fa47 | 2018-09-16 02:08:06 +0100 | [diff] [blame] | 63 | |
Samuel Holland | afe2173 | 2020-12-13 20:05:11 -0600 | [diff] [blame] | 64 | if (u_boot_base[i] != PRELOADED_BL33_BASE) |
Andre Przywara | ea5fa47 | 2018-09-16 02:08:06 +0100 | [diff] [blame] | 65 | continue; |
| 66 | |
| 67 | /* Does the suspected U-Boot size look anyhow reasonable? */ |
| 68 | if (u_boot_base[i + 1] >= 256 * 1024 * 1024) |
| 69 | continue; |
| 70 | |
| 71 | /* end of the image: base address + size */ |
Samuel Holland | f96cb0a | 2022-01-22 22:06:57 -0600 | [diff] [blame] | 72 | dtb_base = (char *)u_boot_base + u_boot_base[i + 1]; |
Andre Przywara | ea5fa47 | 2018-09-16 02:08:06 +0100 | [diff] [blame] | 73 | |
Samuel Holland | f96cb0a | 2022-01-22 22:06:57 -0600 | [diff] [blame] | 74 | if (fdt_check_header(dtb_base) == 0) { |
| 75 | fdt = dtb_base; |
| 76 | return; |
| 77 | } |
Andre Przywara | ea5fa47 | 2018-09-16 02:08:06 +0100 | [diff] [blame] | 78 | } |
Andre Przywara | ea5fa47 | 2018-09-16 02:08:06 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 81 | void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1, |
| 82 | u_register_t arg2, u_register_t arg3) |
| 83 | { |
| 84 | /* Initialize the debug console as soon as possible */ |
| 85 | console_16550_register(SUNXI_UART0_BASE, SUNXI_UART0_CLK_IN_HZ, |
| 86 | SUNXI_UART0_BAUDRATE, &console); |
| 87 | |
Amit Singh Tomar | 2f37224 | 2018-06-20 00:44:50 +0530 | [diff] [blame] | 88 | #ifdef BL32_BASE |
| 89 | /* Populate entry point information for BL32 */ |
| 90 | SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0); |
| 91 | SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE); |
| 92 | bl32_image_ep_info.pc = BL32_BASE; |
| 93 | #endif |
| 94 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 95 | /* Populate entry point information for BL33 */ |
| 96 | SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0); |
| 97 | /* |
| 98 | * Tell BL31 where the non-trusted software image |
| 99 | * is located and the entry state information |
| 100 | */ |
Samuel Holland | afe2173 | 2020-12-13 20:05:11 -0600 | [diff] [blame] | 101 | bl33_image_ep_info.pc = PRELOADED_BL33_BASE; |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 102 | bl33_image_ep_info.spsr = SPSR_64(MODE_EL2, MODE_SP_ELX, |
| 103 | DISABLE_ALL_EXCEPTIONS); |
| 104 | SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE); |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void bl31_plat_arch_setup(void) |
| 108 | { |
| 109 | sunxi_configure_mmu_el3(0); |
| 110 | } |
| 111 | |
| 112 | void bl31_platform_setup(void) |
| 113 | { |
Andre Przywara | c2366b9 | 2018-06-22 00:47:08 +0100 | [diff] [blame] | 114 | const char *soc_name; |
| 115 | uint16_t soc_id = sunxi_read_soc_id(); |
| 116 | |
| 117 | switch (soc_id) { |
Andre Przywara | 78dca1f | 2018-09-17 00:03:09 +0100 | [diff] [blame] | 118 | case SUNXI_SOC_A64: |
Andre Przywara | c2366b9 | 2018-06-22 00:47:08 +0100 | [diff] [blame] | 119 | soc_name = "A64/H64/R18"; |
| 120 | break; |
Andre Przywara | 78dca1f | 2018-09-17 00:03:09 +0100 | [diff] [blame] | 121 | case SUNXI_SOC_H5: |
Andre Przywara | c2366b9 | 2018-06-22 00:47:08 +0100 | [diff] [blame] | 122 | soc_name = "H5"; |
| 123 | break; |
Andre Przywara | 78dca1f | 2018-09-17 00:03:09 +0100 | [diff] [blame] | 124 | case SUNXI_SOC_H6: |
Andre Przywara | aa26f53 | 2017-12-08 01:27:02 +0000 | [diff] [blame] | 125 | soc_name = "H6"; |
| 126 | break; |
Andre Przywara | bafb561 | 2020-11-24 11:07:10 +0000 | [diff] [blame] | 127 | case SUNXI_SOC_H616: |
| 128 | soc_name = "H616"; |
| 129 | break; |
Icenowy Zheng | 61da756 | 2021-07-22 09:41:16 +0800 | [diff] [blame] | 130 | case SUNXI_SOC_R329: |
| 131 | soc_name = "R329"; |
| 132 | break; |
Andre Przywara | c2366b9 | 2018-06-22 00:47:08 +0100 | [diff] [blame] | 133 | default: |
| 134 | soc_name = "unknown"; |
| 135 | break; |
| 136 | } |
| 137 | NOTICE("BL31: Detected Allwinner %s SoC (%04x)\n", soc_name, soc_id); |
| 138 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 139 | generic_delay_timer_init(); |
| 140 | |
Samuel Holland | f96cb0a | 2022-01-22 22:06:57 -0600 | [diff] [blame] | 141 | sunxi_find_dtb(); |
Andre Przywara | ea5fa47 | 2018-09-16 02:08:06 +0100 | [diff] [blame] | 142 | if (fdt) { |
| 143 | const char *model; |
| 144 | int length; |
| 145 | |
| 146 | model = fdt_getprop(fdt, 0, "model", &length); |
| 147 | NOTICE("BL31: Found U-Boot DTB at %p, model: %s\n", fdt, |
| 148 | model ?: "unknown"); |
| 149 | } else { |
| 150 | NOTICE("BL31: No DTB found.\n"); |
| 151 | } |
| 152 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 153 | /* Configure the interrupt controller */ |
| 154 | gicv2_driver_init(&sunxi_gic_data); |
| 155 | gicv2_distif_init(); |
| 156 | gicv2_pcpu_distif_init(); |
| 157 | gicv2_cpuif_enable(); |
| 158 | |
Andre Przywara | 1381547 | 2018-06-01 02:01:39 +0100 | [diff] [blame] | 159 | sunxi_security_setup(); |
| 160 | |
Andre Przywara | e1eb436 | 2018-11-04 23:37:48 +0000 | [diff] [blame] | 161 | /* |
| 162 | * On the A64 U-Boot's SPL sets the bus clocks to some conservative |
| 163 | * values, to work around FEL mode instabilities with SRAM C accesses. |
| 164 | * FEL mode is gone when we reach ATF, so bring the AHB1 bus |
| 165 | * (the "main" bus) clock frequency back to the recommended 200MHz, |
| 166 | * for improved performance. |
| 167 | */ |
| 168 | if (soc_id == SUNXI_SOC_A64) |
| 169 | mmio_write_32(SUNXI_CCU_BASE + 0x54, 0x00003180); |
| 170 | |
| 171 | /* |
| 172 | * U-Boot or the kernel don't setup AHB2, which leaves it at the |
| 173 | * AHB1 frequency (200 MHz, see above). However Allwinner recommends |
| 174 | * 300 MHz, for improved Ethernet and USB performance. Switch the |
| 175 | * clock to use "PLL_PERIPH0 / 2". |
| 176 | */ |
| 177 | if (soc_id == SUNXI_SOC_A64 || soc_id == SUNXI_SOC_H5) |
| 178 | mmio_write_32(SUNXI_CCU_BASE + 0x5c, 0x1); |
| 179 | |
Andre Przywara | 4e4b1e6 | 2018-09-08 19:18:37 +0100 | [diff] [blame] | 180 | sunxi_pmic_setup(soc_id, fdt); |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 181 | |
Samuel Holland | f96cb0a | 2022-01-22 22:06:57 -0600 | [diff] [blame] | 182 | INFO("BL31: Platform setup done\n"); |
| 183 | } |
| 184 | |
| 185 | void bl31_plat_runtime_setup(void) |
| 186 | { |
Andre Przywara | 9de1222 | 2021-12-19 13:39:40 +0000 | [diff] [blame] | 187 | /* Change the DTB if the configuration requires so. */ |
Andre Przywara | fb83833 | 2020-12-14 12:06:24 +0000 | [diff] [blame] | 188 | sunxi_prepare_dtb(fdt); |
| 189 | |
Samuel Holland | f96cb0a | 2022-01-22 22:06:57 -0600 | [diff] [blame] | 190 | console_switch_state(CONSOLE_FLAG_RUNTIME); |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type) |
| 194 | { |
| 195 | assert(sec_state_is_valid(type) != 0); |
Amit Singh Tomar | 2f37224 | 2018-06-20 00:44:50 +0530 | [diff] [blame] | 196 | |
| 197 | if (type == NON_SECURE) |
| 198 | return &bl33_image_ep_info; |
| 199 | |
| 200 | if ((type == SECURE) && bl32_image_ep_info.pc) |
| 201 | return &bl32_image_ep_info; |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 202 | |
Amit Singh Tomar | 2f37224 | 2018-06-20 00:44:50 +0530 | [diff] [blame] | 203 | return NULL; |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 204 | } |