1 /*
2  * Copyright (c) 2014 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Full C support initialization
10  *
11  *
12  * Initialization of full C support: zero the .bss, copy the .data if XIP,
13  * call z_cstart().
14  *
15  * Stack is available in this module, but not the global data/bss until their
16  * initialization is performed.
17  */
18 
19 #include <zephyr/types.h>
20 #include <zephyr/toolchain.h>
21 #include <zephyr/linker/linker-defs.h>
22 #include <zephyr/arch/arc/v2/aux_regs.h>
23 #include <zephyr/arch/arc/cluster.h>
24 #include <zephyr/kernel_structs.h>
25 #include <kernel_internal.h>
26 #include <zephyr/platform/hooks.h>
27 #include <zephyr/arch/cache.h>
28 
29 #ifdef CONFIG_ISA_ARCV3
30 /* NOTE: it will be called from early C code - we must NOT use global / static variables in it! */
arc_cluster_scm_enable(void)31 static void arc_cluster_scm_enable(void)
32 {
33 	unsigned int cluster_version;
34 
35 	/* Check that we have cluster and its version is supported */
36 	cluster_version = z_arc_v2_aux_reg_read(_ARC_REG_CLN_BCR) & _ARC_CLN_BCR_VER_MAJOR_MASK;
37 	if (cluster_version < _ARC_REG_CLN_BCR_VER_MAJOR_ARCV3_MIN) {
38 		return;
39 	}
40 
41 	/* Check that we have shared cache in cluster */
42 	if (!(z_arc_v2_aux_reg_read(_ARC_CLNR_BCR_0) & _ARC_CLNR_BCR_0_HAS_SCM)) {
43 		return;
44 	}
45 
46 	/* Disable SCM, just in case. */
47 	arc_cln_write_reg_nolock(ARC_CLN_CACHE_STATUS, 0);
48 
49 	/* Invalidate SCM before enabling. */
50 	arc_cln_write_reg_nolock(ARC_CLN_CACHE_CMD,
51 				 ARC_CLN_CACHE_CMD_OP_REG_INV | ARC_CLN_CACHE_CMD_INCR);
52 	while (arc_cln_read_reg_nolock(ARC_CLN_CACHE_STATUS) & ARC_CLN_CACHE_STATUS_BUSY) {
53 	}
54 
55 	arc_cln_write_reg_nolock(ARC_CLN_CACHE_STATUS, ARC_CLN_CACHE_STATUS_EN);
56 }
57 #endif /* CONFIG_ISA_ARCV3 */
58 
59 #ifdef __CCAC__
60 extern char __device_states_start[];
61 extern char __device_states_end[];
62 /**
63  * @brief Clear device_states section
64  *
65  * This routine clears the device_states section,
66  * as MW compiler marks the section with NOLOAD flag.
67  */
dev_state_zero(void)68 static void dev_state_zero(void)
69 {
70 	z_early_memset(__device_states_start, 0, __device_states_end - __device_states_start);
71 }
72 #endif
73 
74 extern FUNC_NORETURN void z_cstart(void);
75 extern void arc_mpu_init(void);
76 extern void arc_secureshield_init(void);
77 
78 /**
79  * @brief Prepare to and run C code
80  *
81  * This routine prepares for the execution of and runs C code.
82  */
83 
z_prep_c(void)84 void z_prep_c(void)
85 {
86 #if defined(CONFIG_SOC_PREP_HOOK)
87 	soc_prep_hook();
88 #endif
89 
90 #ifdef CONFIG_ISA_ARCV3
91 	arc_cluster_scm_enable();
92 #endif
93 
94 	z_bss_zero();
95 #ifdef __CCAC__
96 	dev_state_zero();
97 #endif
98 	z_data_copy();
99 #if CONFIG_ARCH_CACHE
100 	arch_cache_init();
101 #endif
102 #ifdef CONFIG_ARC_MPU
103 	arc_mpu_init();
104 #endif
105 #ifdef CONFIG_ARC_SECURE_FIRMWARE
106 	arc_secureshield_init();
107 #endif
108 	z_cstart();
109 	CODE_UNREACHABLE;
110 }
111