1 /*
2  *  SPDX-License-Identifier: BSD-3-Clause
3  *  SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
4  *
5  */
6 
7 #include "hardware/clocks.h"
8 
9 #ifdef TFM_MULTI_CORE_TOPOLOGY
10 #include "hardware/structs/sio.h"
11 #include "hardware/structs/scb.h"
12 #include "tfm_multi_core_api.h"
13 #endif
14 
15 #include "stdint.h"
16 
17 /* Do not use __cmsis_start */
18 #define __PROGRAM_START
19 #include "tfm_hal_device_header.h"
20 
copy_zero_tables(void)21 void copy_zero_tables(void) {
22     typedef struct {
23         uint32_t const* src;
24         uint32_t* dest;
25         uint32_t  wlen;
26     } __copy_table_t;
27 
28     typedef struct {
29         uint32_t* dest;
30         uint32_t  wlen;
31     } __zero_table_t;
32 
33     extern const __copy_table_t __copy_table_start__;
34     extern const __copy_table_t __copy_table_end__;
35     extern const __zero_table_t __zero_table_start__;
36     extern const __zero_table_t __zero_table_end__;
37 
38     for (__copy_table_t const* pTable = &__copy_table_start__; pTable < &__copy_table_end__; ++pTable) {
39         for(uint32_t i=0u; i<pTable->wlen; ++i) {
40             pTable->dest[i] = pTable->src[i];
41         }
42     }
43 
44     for (__zero_table_t const* pTable = &__zero_table_start__; pTable < &__zero_table_end__; ++pTable) {
45         for(uint32_t i=0u; i<pTable->wlen; ++i) {
46             pTable->dest[i] = 0u;
47         }
48     }
49 }
50 
hard_assertion_failure(void)51 void __weak hard_assertion_failure(void) {
52     panic("Hard assert");
53 }
54 
55 extern void runtime_init_install_ram_vector_table(void);
56 extern uint32_t ram_vector_table[PICO_RAM_VECTOR_TABLE_SIZE];
57 
runtime_init(void)58 void runtime_init(void) {
59 #ifdef TFM_MULTI_CORE_TOPOLOGY
60     if(sio_hw->cpuid == 0) {
61         scb_hw->vtor = (uintptr_t) ram_vector_table;
62         return;
63     }
64 #endif
65     copy_zero_tables();
66     runtime_init_install_ram_vector_table();
67 
68     /* These are already configured by the Secure side, just fill the array */
69     clock_set_reported_hz(clk_ref, XOSC_KHZ * KHZ);
70     clock_set_reported_hz(clk_sys, SYS_CLK_KHZ * KHZ);
71     clock_set_reported_hz(clk_peri, SYS_CLK_KHZ * KHZ);
72     clock_set_reported_hz(clk_hstx, SYS_CLK_KHZ * KHZ);
73     clock_set_reported_hz(clk_usb, USB_CLK_KHZ * KHZ);
74     clock_set_reported_hz(clk_adc, USB_CLK_KHZ * KHZ);
75 
76     SystemInit();
77 }
78 
79 #ifdef TFM_MULTI_CORE_TOPOLOGY
tfm_ns_wait_for_s_cpu_ready(void)80 int32_t tfm_ns_wait_for_s_cpu_ready(void)
81 {
82     return tfm_platform_ns_wait_for_s_cpu_ready();
83 }
84 #endif
85