1 /*
2  * Copyright (c) 2021-2024, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <stdint.h>
9 
10 #include "tfm_hal_device_header.h"
11 #include "target_cfg.h"
12 #include "tfm_hal_platform.h"
13 #include "tfm_plat_defs.h"
14 #include "uart_stdout.h"
15 
16 extern const struct memory_region_limits memory_regions;
17 
tfm_hal_platform_init(void)18 enum tfm_hal_status_t tfm_hal_platform_init(void)
19 {
20     enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
21 
22     plat_err = enable_fault_handlers();
23     if (plat_err != TFM_PLAT_ERR_SUCCESS) {
24         return TFM_HAL_ERROR_GENERIC;
25     }
26 
27     plat_err = system_reset_cfg();
28     if (plat_err != TFM_PLAT_ERR_SUCCESS) {
29         return TFM_HAL_ERROR_GENERIC;
30     }
31 
32     plat_err = init_debug();
33     if (plat_err != TFM_PLAT_ERR_SUCCESS) {
34         return TFM_HAL_ERROR_GENERIC;
35     }
36 
37     __enable_irq();
38     stdio_init();
39 
40     plat_err = nvic_interrupt_target_state_cfg();
41     if (plat_err != TFM_PLAT_ERR_SUCCESS) {
42         return TFM_HAL_ERROR_GENERIC;
43     }
44 
45     plat_err = nvic_interrupt_enable();
46     if (plat_err != TFM_PLAT_ERR_SUCCESS) {
47         return TFM_HAL_ERROR_GENERIC;
48     }
49 
50     return TFM_HAL_SUCCESS;
51 }
52 
tfm_hal_system_reset(void)53 void tfm_hal_system_reset(void)
54 {
55     __disable_irq();
56     mpc_revert_non_secure_to_secure_cfg();
57 
58     NVIC->ICPR[0] = UINT32_MAX;         /* Clear all pending interrupts */
59     NVIC->ICPR[1] = UINT32_MAX;         /* Clear all pending interrupts */
60     NVIC->ICPR[2] = UINT32_MAX;         /* Clear all pending interrupts */
61     NVIC->ICPR[3] = UINT32_MAX;         /* Clear all pending interrupts */
62     NVIC->ICPR[4] = UINT32_MAX;         /* Clear all pending interrupts */
63     NVIC->ICPR[5] = UINT32_MAX;         /* Clear all pending interrupts */
64     NVIC->ICPR[6] = UINT32_MAX;         /* Clear all pending interrupts */
65     NVIC->ICPR[7] = UINT32_MAX;         /* Clear all pending interrupts */
66 
67     NVIC_SystemReset();
68 }
69 
tfm_hal_system_halt(void)70 void tfm_hal_system_halt(void)
71 {
72     /*
73      * Disable IRQs to stop all threads, not just the thread that
74      * halted the system.
75      */
76     __disable_irq();
77 
78     /*
79      * Enter sleep to reduce power consumption and do it in a loop in
80      * case a signal wakes up the CPU.
81      */
82     while (1) {
83         __WFE();
84     }
85 }
86 
tfm_hal_get_ns_VTOR(void)87 uint32_t tfm_hal_get_ns_VTOR(void)
88 {
89     return memory_regions.non_secure_code_start;
90 }
91 
tfm_hal_get_ns_MSP(void)92 uint32_t tfm_hal_get_ns_MSP(void)
93 {
94     return *((uint32_t *)memory_regions.non_secure_code_start);
95 }
96 
tfm_hal_get_ns_entry_point(void)97 uint32_t tfm_hal_get_ns_entry_point(void)
98 {
99     return *((uint32_t *)(memory_regions.non_secure_code_start + 4));
100 }
101