1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved. 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu #include <assert.h> 8*91f16700Schasinglulu 9*91f16700Schasinglulu #include <platform_def.h> 10*91f16700Schasinglulu 11*91f16700Schasinglulu #include <arch_helpers.h> 12*91f16700Schasinglulu #include <common/bl_common.h> 13*91f16700Schasinglulu #include <common/debug.h> 14*91f16700Schasinglulu #include <bl31/interrupt_mgmt.h> 15*91f16700Schasinglulu #include <drivers/console.h> 16*91f16700Schasinglulu #include <drivers/rpi3/gpio/rpi3_gpio.h> 17*91f16700Schasinglulu #include <drivers/ti/uart/uart_16550.h> 18*91f16700Schasinglulu #include <drivers/arm/pl011.h> 19*91f16700Schasinglulu #include <lib/xlat_tables/xlat_tables_v2.h> 20*91f16700Schasinglulu 21*91f16700Schasinglulu #include <rpi_hw.h> 22*91f16700Schasinglulu #include <rpi_shared.h> 23*91f16700Schasinglulu 24*91f16700Schasinglulu #define MAP_DEVICE0 MAP_REGION_FLAT(DEVICE0_BASE, \ 25*91f16700Schasinglulu DEVICE0_SIZE, \ 26*91f16700Schasinglulu MT_DEVICE | MT_RW | MT_SECURE) 27*91f16700Schasinglulu 28*91f16700Schasinglulu #ifdef SHARED_RAM_BASE 29*91f16700Schasinglulu #define MAP_SHARED_RAM MAP_REGION_FLAT(SHARED_RAM_BASE, \ 30*91f16700Schasinglulu SHARED_RAM_SIZE, \ 31*91f16700Schasinglulu MT_DEVICE | MT_RW | MT_SECURE) 32*91f16700Schasinglulu #endif 33*91f16700Schasinglulu 34*91f16700Schasinglulu #ifdef RPI3_PRELOADED_DTB_BASE 35*91f16700Schasinglulu #define MAP_NS_DTB MAP_REGION_FLAT(RPI3_PRELOADED_DTB_BASE, 0x10000, \ 36*91f16700Schasinglulu MT_MEMORY | MT_RW | MT_NS) 37*91f16700Schasinglulu #endif 38*91f16700Schasinglulu 39*91f16700Schasinglulu #define MAP_NS_DRAM0 MAP_REGION_FLAT(NS_DRAM0_BASE, NS_DRAM0_SIZE, \ 40*91f16700Schasinglulu MT_MEMORY | MT_RW | MT_NS) 41*91f16700Schasinglulu 42*91f16700Schasinglulu #define MAP_FIP MAP_REGION_FLAT(PLAT_RPI3_FIP_BASE, \ 43*91f16700Schasinglulu PLAT_RPI3_FIP_MAX_SIZE, \ 44*91f16700Schasinglulu MT_MEMORY | MT_RO | MT_NS) 45*91f16700Schasinglulu 46*91f16700Schasinglulu #define MAP_BL32_MEM MAP_REGION_FLAT(BL32_MEM_BASE, BL32_MEM_SIZE, \ 47*91f16700Schasinglulu MT_MEMORY | MT_RW | MT_SECURE) 48*91f16700Schasinglulu 49*91f16700Schasinglulu #ifdef SPD_opteed 50*91f16700Schasinglulu #define MAP_OPTEE_PAGEABLE MAP_REGION_FLAT( \ 51*91f16700Schasinglulu RPI3_OPTEE_PAGEABLE_LOAD_BASE, \ 52*91f16700Schasinglulu RPI3_OPTEE_PAGEABLE_LOAD_SIZE, \ 53*91f16700Schasinglulu MT_MEMORY | MT_RW | MT_SECURE) 54*91f16700Schasinglulu #endif 55*91f16700Schasinglulu 56*91f16700Schasinglulu /* 57*91f16700Schasinglulu * Table of regions for various BL stages to map using the MMU. 58*91f16700Schasinglulu */ 59*91f16700Schasinglulu #ifdef IMAGE_BL1 60*91f16700Schasinglulu static const mmap_region_t plat_rpi3_mmap[] = { 61*91f16700Schasinglulu #ifdef MAP_SHARED_RAM 62*91f16700Schasinglulu MAP_SHARED_RAM, 63*91f16700Schasinglulu #endif 64*91f16700Schasinglulu MAP_DEVICE0, 65*91f16700Schasinglulu MAP_FIP, 66*91f16700Schasinglulu #ifdef SPD_opteed 67*91f16700Schasinglulu MAP_OPTEE_PAGEABLE, 68*91f16700Schasinglulu #endif 69*91f16700Schasinglulu {0} 70*91f16700Schasinglulu }; 71*91f16700Schasinglulu #endif 72*91f16700Schasinglulu 73*91f16700Schasinglulu #ifdef IMAGE_BL2 74*91f16700Schasinglulu static const mmap_region_t plat_rpi3_mmap[] = { 75*91f16700Schasinglulu #ifdef MAP_SHARED_RAM 76*91f16700Schasinglulu MAP_SHARED_RAM, 77*91f16700Schasinglulu #endif 78*91f16700Schasinglulu MAP_DEVICE0, 79*91f16700Schasinglulu MAP_FIP, 80*91f16700Schasinglulu MAP_NS_DRAM0, 81*91f16700Schasinglulu #ifdef BL32_BASE 82*91f16700Schasinglulu MAP_BL32_MEM, 83*91f16700Schasinglulu #endif 84*91f16700Schasinglulu {0} 85*91f16700Schasinglulu }; 86*91f16700Schasinglulu #endif 87*91f16700Schasinglulu 88*91f16700Schasinglulu #ifdef IMAGE_BL31 89*91f16700Schasinglulu static const mmap_region_t plat_rpi3_mmap[] = { 90*91f16700Schasinglulu #ifdef MAP_SHARED_RAM 91*91f16700Schasinglulu MAP_SHARED_RAM, 92*91f16700Schasinglulu #endif 93*91f16700Schasinglulu MAP_DEVICE0, 94*91f16700Schasinglulu #ifdef RPI3_PRELOADED_DTB_BASE 95*91f16700Schasinglulu MAP_NS_DTB, 96*91f16700Schasinglulu #endif 97*91f16700Schasinglulu #ifdef BL32_BASE 98*91f16700Schasinglulu MAP_BL32_MEM, 99*91f16700Schasinglulu #endif 100*91f16700Schasinglulu {0} 101*91f16700Schasinglulu }; 102*91f16700Schasinglulu #endif 103*91f16700Schasinglulu 104*91f16700Schasinglulu /******************************************************************************* 105*91f16700Schasinglulu * Function that sets up the console 106*91f16700Schasinglulu ******************************************************************************/ 107*91f16700Schasinglulu static console_t rpi3_console; 108*91f16700Schasinglulu 109*91f16700Schasinglulu 110*91f16700Schasinglulu static bool rpi3_use_mini_uart(void) 111*91f16700Schasinglulu { 112*91f16700Schasinglulu return rpi3_gpio_get_select(14) == RPI3_GPIO_FUNC_ALT5; 113*91f16700Schasinglulu } 114*91f16700Schasinglulu 115*91f16700Schasinglulu void rpi3_console_init(void) 116*91f16700Schasinglulu { 117*91f16700Schasinglulu int console_scope = CONSOLE_FLAG_BOOT; 118*91f16700Schasinglulu int rc; 119*91f16700Schasinglulu 120*91f16700Schasinglulu if (RPI3_RUNTIME_UART != -1) 121*91f16700Schasinglulu console_scope |= CONSOLE_FLAG_RUNTIME; 122*91f16700Schasinglulu 123*91f16700Schasinglulu rpi3_gpio_init(); 124*91f16700Schasinglulu 125*91f16700Schasinglulu if (rpi3_use_mini_uart()) 126*91f16700Schasinglulu rc = console_16550_register(PLAT_RPI_MINI_UART_BASE, 127*91f16700Schasinglulu 0, 128*91f16700Schasinglulu PLAT_RPI_UART_BAUDRATE, 129*91f16700Schasinglulu &rpi3_console); 130*91f16700Schasinglulu else 131*91f16700Schasinglulu rc = console_pl011_register(PLAT_RPI_PL011_UART_BASE, 132*91f16700Schasinglulu PLAT_RPI_PL011_UART_CLOCK, 133*91f16700Schasinglulu PLAT_RPI_UART_BAUDRATE, 134*91f16700Schasinglulu &rpi3_console); 135*91f16700Schasinglulu 136*91f16700Schasinglulu if (rc == 0) { 137*91f16700Schasinglulu /* 138*91f16700Schasinglulu * The crash console doesn't use the multi console API, it uses 139*91f16700Schasinglulu * the core console functions directly. It is safe to call panic 140*91f16700Schasinglulu * and let it print debug information. 141*91f16700Schasinglulu */ 142*91f16700Schasinglulu panic(); 143*91f16700Schasinglulu } 144*91f16700Schasinglulu 145*91f16700Schasinglulu console_set_scope(&rpi3_console, console_scope); 146*91f16700Schasinglulu } 147*91f16700Schasinglulu 148*91f16700Schasinglulu /******************************************************************************* 149*91f16700Schasinglulu * Function that sets up the translation tables. 150*91f16700Schasinglulu ******************************************************************************/ 151*91f16700Schasinglulu void rpi3_setup_page_tables(uintptr_t total_base, size_t total_size, 152*91f16700Schasinglulu uintptr_t code_start, uintptr_t code_limit, 153*91f16700Schasinglulu uintptr_t rodata_start, uintptr_t rodata_limit 154*91f16700Schasinglulu #if USE_COHERENT_MEM 155*91f16700Schasinglulu , uintptr_t coh_start, uintptr_t coh_limit 156*91f16700Schasinglulu #endif 157*91f16700Schasinglulu ) 158*91f16700Schasinglulu { 159*91f16700Schasinglulu /* 160*91f16700Schasinglulu * Map the Trusted SRAM with appropriate memory attributes. 161*91f16700Schasinglulu * Subsequent mappings will adjust the attributes for specific regions. 162*91f16700Schasinglulu */ 163*91f16700Schasinglulu VERBOSE("Trusted SRAM seen by this BL image: %p - %p\n", 164*91f16700Schasinglulu (void *) total_base, (void *) (total_base + total_size)); 165*91f16700Schasinglulu mmap_add_region(total_base, total_base, 166*91f16700Schasinglulu total_size, 167*91f16700Schasinglulu MT_MEMORY | MT_RW | MT_SECURE); 168*91f16700Schasinglulu 169*91f16700Schasinglulu /* Re-map the code section */ 170*91f16700Schasinglulu VERBOSE("Code region: %p - %p\n", 171*91f16700Schasinglulu (void *) code_start, (void *) code_limit); 172*91f16700Schasinglulu mmap_add_region(code_start, code_start, 173*91f16700Schasinglulu code_limit - code_start, 174*91f16700Schasinglulu MT_CODE | MT_SECURE); 175*91f16700Schasinglulu 176*91f16700Schasinglulu /* Re-map the read-only data section */ 177*91f16700Schasinglulu VERBOSE("Read-only data region: %p - %p\n", 178*91f16700Schasinglulu (void *) rodata_start, (void *) rodata_limit); 179*91f16700Schasinglulu mmap_add_region(rodata_start, rodata_start, 180*91f16700Schasinglulu rodata_limit - rodata_start, 181*91f16700Schasinglulu MT_RO_DATA | MT_SECURE); 182*91f16700Schasinglulu 183*91f16700Schasinglulu #if USE_COHERENT_MEM 184*91f16700Schasinglulu /* Re-map the coherent memory region */ 185*91f16700Schasinglulu VERBOSE("Coherent region: %p - %p\n", 186*91f16700Schasinglulu (void *) coh_start, (void *) coh_limit); 187*91f16700Schasinglulu mmap_add_region(coh_start, coh_start, 188*91f16700Schasinglulu coh_limit - coh_start, 189*91f16700Schasinglulu MT_DEVICE | MT_RW | MT_SECURE); 190*91f16700Schasinglulu #endif 191*91f16700Schasinglulu 192*91f16700Schasinglulu mmap_add(plat_rpi3_mmap); 193*91f16700Schasinglulu 194*91f16700Schasinglulu init_xlat_tables(); 195*91f16700Schasinglulu } 196*91f16700Schasinglulu 197*91f16700Schasinglulu /******************************************************************************* 198*91f16700Schasinglulu * Gets SPSR for BL32 entry 199*91f16700Schasinglulu ******************************************************************************/ 200*91f16700Schasinglulu uint32_t rpi3_get_spsr_for_bl32_entry(void) 201*91f16700Schasinglulu { 202*91f16700Schasinglulu /* 203*91f16700Schasinglulu * The Secure Payload Dispatcher service is responsible for 204*91f16700Schasinglulu * setting the SPSR prior to entry into the BL32 image. 205*91f16700Schasinglulu */ 206*91f16700Schasinglulu return 0; 207*91f16700Schasinglulu } 208*91f16700Schasinglulu 209*91f16700Schasinglulu /******************************************************************************* 210*91f16700Schasinglulu * Gets SPSR for BL33 entry 211*91f16700Schasinglulu ******************************************************************************/ 212*91f16700Schasinglulu uint32_t rpi3_get_spsr_for_bl33_entry(void) 213*91f16700Schasinglulu { 214*91f16700Schasinglulu #if RPI3_BL33_IN_AARCH32 215*91f16700Schasinglulu INFO("BL33 will boot in Non-secure AArch32 Hypervisor mode\n"); 216*91f16700Schasinglulu return SPSR_MODE32(MODE32_hyp, SPSR_T_ARM, SPSR_E_LITTLE, 217*91f16700Schasinglulu DISABLE_ALL_EXCEPTIONS); 218*91f16700Schasinglulu #else 219*91f16700Schasinglulu return SPSR_64(MODE_EL2, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); 220*91f16700Schasinglulu #endif 221*91f16700Schasinglulu } 222*91f16700Schasinglulu 223*91f16700Schasinglulu unsigned int plat_get_syscnt_freq2(void) 224*91f16700Schasinglulu { 225*91f16700Schasinglulu return SYS_COUNTER_FREQ_IN_TICKS; 226*91f16700Schasinglulu } 227*91f16700Schasinglulu 228*91f16700Schasinglulu uint32_t plat_ic_get_pending_interrupt_type(void) 229*91f16700Schasinglulu { 230*91f16700Schasinglulu ERROR("rpi3: Interrupt routed to EL3.\n"); 231*91f16700Schasinglulu return INTR_TYPE_INVAL; 232*91f16700Schasinglulu } 233*91f16700Schasinglulu 234*91f16700Schasinglulu uint32_t plat_interrupt_type_to_line(uint32_t type, uint32_t security_state) 235*91f16700Schasinglulu { 236*91f16700Schasinglulu assert((type == INTR_TYPE_S_EL1) || (type == INTR_TYPE_EL3) || 237*91f16700Schasinglulu (type == INTR_TYPE_NS)); 238*91f16700Schasinglulu 239*91f16700Schasinglulu assert(sec_state_is_valid(security_state)); 240*91f16700Schasinglulu 241*91f16700Schasinglulu /* Non-secure interrupts are signalled on the IRQ line always. */ 242*91f16700Schasinglulu if (type == INTR_TYPE_NS) 243*91f16700Schasinglulu return __builtin_ctz(SCR_IRQ_BIT); 244*91f16700Schasinglulu 245*91f16700Schasinglulu /* Secure interrupts are signalled on the FIQ line always. */ 246*91f16700Schasinglulu return __builtin_ctz(SCR_FIQ_BIT); 247*91f16700Schasinglulu } 248