1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Texas Instruments K3 SA2UL Driver 4 * 5 * Copyright (C) 2022 Texas Instruments Incorporated - https://www.ti.com/ 6 * Andrew Davis <afd@ti.com> 7 */ 8 9 #include <drivers/ti_sci.h> 10 #include <initcall.h> 11 #include <io.h> 12 #include <keep.h> 13 #include <kernel/interrupt.h> 14 #include <kernel/misc.h> 15 #include <kernel/spinlock.h> 16 #include <mm/core_memprot.h> 17 #include <mm/core_mmu.h> 18 #include <platform_config.h> 19 #include <rng_support.h> 20 21 #include "eip76d_trng.h" 22 #include "ti_crypto.h" 23 24 #define SA2UL_ES 0x0008 25 #define SA2UL_ES_TRNG BIT(3) 26 #define SA2UL_EEC 0x1000 27 #define SA2UL_EEC_TRNG BIT(3) 28 29 register_phys_mem_pgdir(MEM_AREA_IO_SEC, SA2UL_BASE, SA2UL_REG_SIZE); 30 sa2ul_init(void)31static TEE_Result sa2ul_init(void) 32 { 33 vaddr_t sa2ul = (vaddr_t)phys_to_virt(SA2UL_BASE, MEM_AREA_IO_SEC, 34 RNG_REG_SIZE); 35 uint32_t val = 0; 36 TEE_Result result = TEE_SUCCESS; 37 int ret = 0; 38 39 if (SA2UL_TI_SCI_DEV_ID != -1) { 40 /* Power on the SA2UL device */ 41 ret = ti_sci_device_get(SA2UL_TI_SCI_DEV_ID); 42 if (ret) { 43 EMSG("Failed to get SA2UL device"); 44 return TEE_ERROR_GENERIC; 45 } 46 } 47 48 IMSG("Activated SA2UL device"); 49 50 result = ti_crypto_init_rng_fwl(SA2UL_TI_SCI_FW_ID, 51 SA2UL_TI_SCI_FW_RGN_ID); 52 if (result != TEE_SUCCESS) { 53 EMSG("Failed to enable firewalls for TRNG device"); 54 return TEE_ERROR_GENERIC; 55 } 56 57 IMSG("Enabled firewalls for SA2UL TRNG device"); 58 59 /* Enable RNG engine in SA2UL if not already enabled */ 60 val = io_read32(sa2ul + SA2UL_ES); 61 if (!(val & SA2UL_ES_TRNG)) { 62 IMSG("Enabling SA2UL TRNG engine"); 63 io_setbits32(sa2ul + SA2UL_EEC, SA2UL_EEC_TRNG); 64 } 65 66 /* Initialize the RNG Module */ 67 result = eip76d_rng_init(); 68 if (result != TEE_SUCCESS) 69 return result; 70 71 IMSG("SA2UL Drivers initialized"); 72 73 return TEE_SUCCESS; 74 } 75 service_init_crypto(sa2ul_init); 76