1 /*
2 
3 Copyright (c) 2009-2024 ARM Limited. All rights reserved.
4 
5     SPDX-License-Identifier: Apache-2.0
6 
7 Licensed under the Apache License, Version 2.0 (the License); you may
8 not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10 
11     www.apache.org/licenses/LICENSE-2.0
12 
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an AS IS BASIS, WITHOUT
15 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 
19 NOTICE: This file has been modified by Nordic Semiconductor ASA.
20 
21 */
22 
23 /* NOTE: Template files (including this one) are application specific and therefore expected to
24    be copied into the application project folder prior to its use! */
25 
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include "nrf.h"
29 #include "system_nrf54l.h"
30 #include "system_nrf54l_approtect.h"
31 #include "system_config_sau.h"
32 
33 /*lint ++flb "Enter library region" */
34 
35 #define __SYSTEM_CLOCK_DEFAULT      (64000000ul)
36 
37 #if defined ( __CC_ARM ) || defined ( __GNUC__ )
38     uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_DEFAULT;
39 #elif defined ( __ICCARM__ )
40     __root uint32_t SystemCoreClock = __SYSTEM_CLOCK_DEFAULT;
41 #endif
42 
SystemCoreClockUpdate(void)43 void SystemCoreClockUpdate(void)
44 {
45     switch(NRF_OSCILLATORS->PLL.CURRENTFREQ)
46     {
47         case OSCILLATORS_PLL_CURRENTFREQ_CURRENTFREQ_CK64M:
48             SystemCoreClock = 64000000ul;
49             break;
50         case OSCILLATORS_PLL_CURRENTFREQ_CURRENTFREQ_CK128M:
51             SystemCoreClock = 128000000ul;
52             break;
53     }
54 }
55 
SystemInit(void)56 void SystemInit(void)
57 {
58     #ifdef __CORTEX_M
59         #ifndef NRF_SKIP_CLOCK_CONFIGURATION
60             #if defined(NRF_CONFIG_CPU_FREQ_MHZ) && (NRF_CONFIG_CPU_FREQ_MHZ==64)
61                 NRF_OSCILLATORS->PLL.FREQ = OSCILLATORS_PLL_FREQ_FREQ_CK64M;
62             #elif defined(NRF_CONFIG_CPU_FREQ_MHZ) && (NRF_CONFIG_CPU_FREQ_MHZ==128)
63                 NRF_OSCILLATORS->PLL.FREQ = OSCILLATORS_PLL_FREQ_FREQ_CK128M;
64             #elif defined(NRF_CONFIG_CPU_FREQ_MHZ)
65                 #error "Illegal CPU frequency set"
66             #else
67                 NRF_OSCILLATORS->PLL.FREQ = OSCILLATORS_PLL_FREQ_FREQ_CK128M;
68             #endif
69         #endif
70 
71         #if !defined(NRF_TRUSTZONE_NONSECURE) && defined(__ARM_FEATURE_CMSE)
72             #ifndef NRF_SKIP_TAMPC_SETUP
73                 nrf54l_handle_approtect();
74             #endif
75             #if defined(__FPU_PRESENT) && __FPU_PRESENT
76                 /* Allow Non-Secure code to run FPU instructions.
77                 * If only the secure code should control FPU power state these registers should be configured accordingly in the secure application code. */
78                 SCB->NSACR |= (3UL << 10ul);
79             #endif
80 
81             #ifndef NRF_SKIP_SAU_CONFIGURATION
82                 configure_default_sau();
83             #endif
84         #endif
85 
86         #if !defined (NRF_DISABLE_FICR_TRIMCNF)
87             /* Trimming of the device. Copy all the trimming values from FICR into the target addresses. Trim
88                until one ADDR is not initialized. */
89             uint32_t index = 0ul;
90             for (index = 0ul; index < FICR_TRIMCNF_MaxCount && NRF_FICR_NS->TRIMCNF[index].ADDR != 0xFFFFFFFFul && NRF_FICR_NS->TRIMCNF[index].ADDR != 0x00000000ul; index++) {
91             #if defined ( __ICCARM__ )
92                 /* IAR will complain about the order of volatile pointer accesses. */
93                 #pragma diag_suppress=Pa082
94             #endif
95             * ((volatile uint32_t*)NRF_FICR_NS->TRIMCNF[index].ADDR) = NRF_FICR_NS->TRIMCNF[index].DATA;
96             #if defined ( __ICCARM__ )
97                 #pragma diag_default=Pa082
98             #endif
99             }
100         #endif
101 
102         /* Enable the FPU if the compiler used floating point unit instructions. __FPU_USED is a MACRO defined by the
103         * compiler. Since the FPU consumes energy, remember to disable FPU use in the compiler if floating point unit
104         * operations are not used in your code. */
105 
106         /* Allow Non-Secure code to run FPU instructions.
107          * If only the secure code should control FPU power state these registers should be configured accordingly in the secure application code. */
108         SCB->NSACR |= (3UL << 10ul);
109 
110         #if (__FPU_USED == 1ul)
111             SCB->CPACR |= (3UL << 20ul) | (3UL << 22ul);
112             __DSB();
113             __ISB();
114         #endif
115 
116         #if !defined(NRF_TRUSTZONE_NONSECURE) && defined(__ARM_FEATURE_CMSE)
117             #if defined(NRF_CONFIG_NFCT_PINS_AS_GPIOS)
118                 NRF_NFCT_S->PADCONFIG = (NFCT_PADCONFIG_ENABLE_Disabled << NFCT_PADCONFIG_ENABLE_Pos);
119             #endif
120 
121             /* Enable SWO trace functionality. If ENABLE_SWO is not defined, SWO pin will be used as GPIO (see Product
122             Specification to see which one). */
123             #if defined (ENABLE_SWO)
124                         // Enable Trace And Debug peripheral
125                 NRF_TAD_S->ENABLE = TAD_ENABLE_ENABLE_Msk;
126                 NRF_TAD_S->CLOCKSTART = TAD_CLOCKSTART_START_Msk;
127 
128                 // Set up Trace pad SPU firewall
129                 NRF_SPU_S->GPIOPORT[0].PERM &= ~(1ul << TRACE_TRACEDATA0_PIN);
130 
131                 // Configure trace port pad
132                 NRF_P0_S->PIN_CNF[TRACE_TRACEDATA0_PIN] = TRACE_PIN_CNF_VALUE;
133 
134                 // Select trace pin
135                 NRF_TAD_S->PSEL.TRACEDATA0 = TRACE_TRACEDATA0_PIN;
136 
137                 // Set trace port speed to 64 MHz
138                 NRF_TAD_S->TRACEPORTSPEED = TAD_TRACEPORTSPEED_TRACEPORTSPEED_64MHz;
139             #endif
140 
141                 /* Enable Trace functionality. If ENABLE_TRACE is not defined, TRACE pins will be used as GPIOs (see Product
142                 Specification to see which ones). */
143             #if defined (ENABLE_TRACE)
144                 // Enable Trace And Debug peripheral
145                 NRF_TAD_S->ENABLE = TAD_ENABLE_ENABLE_Msk;
146                 NRF_TAD_S->CLOCKSTART = TAD_CLOCKSTART_START_Msk;
147 
148                 // Set up Trace pads SPU firewall
149                 NRF_SPU_S->GPIOPORT[0].PERM &= ~(1ul << TRACE_TRACECLK_PIN);
150                 NRF_SPU_S->GPIOPORT[0].PERM &= ~(1ul << TRACE_TRACEDATA0_PIN);
151                 NRF_SPU_S->GPIOPORT[0].PERM &= ~(1ul << TRACE_TRACEDATA1_PIN);
152                 NRF_SPU_S->GPIOPORT[0].PERM &= ~(1ul << TRACE_TRACEDATA2_PIN);
153                 NRF_SPU_S->GPIOPORT[0].PERM &= ~(1ul << TRACE_TRACEDATA3_PIN);
154 
155                 // Configure trace port pads
156                 NRF_P0_S->PIN_CNF[TRACE_TRACECLK_PIN] = TRACE_PIN_CNF_VALUE;
157                 NRF_P0_S->PIN_CNF[TRACE_TRACEDATA0_PIN] = TRACE_PIN_CNF_VALUE;
158                 NRF_P0_S->PIN_CNF[TRACE_TRACEDATA1_PIN] = TRACE_PIN_CNF_VALUE;
159                 NRF_P0_S->PIN_CNF[TRACE_TRACEDATA2_PIN] = TRACE_PIN_CNF_VALUE;
160                 NRF_P0_S->PIN_CNF[TRACE_TRACEDATA3_PIN] = TRACE_PIN_CNF_VALUE;
161 
162                 // Select trace pins
163                 NRF_TAD_S->PSEL.TRACECLK = TRACE_TRACECLK_PIN;
164                 NRF_TAD_S->PSEL.TRACEDATA0 = TRACE_TRACEDATA0_PIN;
165                 NRF_TAD_S->PSEL.TRACEDATA1 = TRACE_TRACEDATA1_PIN;
166                 NRF_TAD_S->PSEL.TRACEDATA2 = TRACE_TRACEDATA2_PIN;
167                 NRF_TAD_S->PSEL.TRACEDATA3 = TRACE_TRACEDATA3_PIN;
168 
169                 // Set trace port speed to 64 MHz
170                 NRF_TAD_S->TRACEPORTSPEED = TAD_TRACEPORTSPEED_TRACEPORTSPEED_64MHz;
171             #endif
172         #endif
173 
174         #if !defined(NRF_TRUSTZONE_NONSECURE) && !defined (NRF_SKIP_GLITCHDETECTOR_DISABLE)
175             /* Disable glitch detector */
176             #if defined (GLITCHDET_GLITCHDETECTORS)
177                 NRF_GLITCHDET_S->GLITCHDETECTOR.CONFIG = (GLITCHDET_GLITCHDETECTOR_CONFIG_ENABLE_Disable << GLITCHDET_GLITCHDETECTOR_CONFIG_ENABLE_Pos);
178             #else
179                 NRF_GLITCHDET_S->CONFIG = (GLITCHDET_CONFIG_ENABLE_Disable << GLITCHDET_CONFIG_ENABLE_Pos);
180             #endif
181         #endif
182     #endif
183 }
184 
185 /*lint --flb "Leave library region" */
186