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 <zephyr/arch/common/xip.h>
26 #include <zephyr/arch/common/init.h>
27 #include <zephyr/platform/hooks.h>
28 #include <zephyr/arch/cache.h>
29 
30 #ifdef CONFIG_ISA_ARCV3
31 /* NOTE: it will be called from early C code - we must NOT use global / static variables in it! */
arc_cluster_scm_enable(void)32 static void arc_cluster_scm_enable(void)
33 {
34 	unsigned int cluster_version;
35 
36 	/* Check that we have cluster and its version is supported */
37 	cluster_version = z_arc_v2_aux_reg_read(_ARC_REG_CLN_BCR) & _ARC_CLN_BCR_VER_MAJOR_MASK;
38 	if (cluster_version < _ARC_REG_CLN_BCR_VER_MAJOR_ARCV3_MIN) {
39 		return;
40 	}
41 
42 	/* Check that we have shared cache in cluster */
43 	if (!(z_arc_v2_aux_reg_read(_ARC_CLNR_BCR_0) & _ARC_CLNR_BCR_0_HAS_SCM)) {
44 		return;
45 	}
46 
47 	/* Disable SCM, just in case. */
48 	arc_cln_write_reg_nolock(ARC_CLN_CACHE_STATUS, 0);
49 
50 	/* Invalidate SCM before enabling. */
51 	arc_cln_write_reg_nolock(ARC_CLN_CACHE_CMD,
52 				 ARC_CLN_CACHE_CMD_OP_REG_INV | ARC_CLN_CACHE_CMD_INCR);
53 	while (arc_cln_read_reg_nolock(ARC_CLN_CACHE_STATUS) & ARC_CLN_CACHE_STATUS_BUSY) {
54 	}
55 
56 	arc_cln_write_reg_nolock(ARC_CLN_CACHE_STATUS, ARC_CLN_CACHE_STATUS_EN);
57 }
58 #endif /* CONFIG_ISA_ARCV3 */
59 
60 #ifdef __CCAC__
61 extern char __device_states_start[];
62 extern char __device_states_end[];
63 /**
64  * @brief Clear device_states section
65  *
66  * This routine clears the device_states section,
67  * as MW compiler marks the section with NOLOAD flag.
68  */
dev_state_zero(void)69 static void dev_state_zero(void)
70 {
71 	arch_early_memset(__device_states_start, 0, __device_states_end - __device_states_start);
72 }
73 #endif
74 
75 extern FUNC_NORETURN void z_cstart(void);
76 extern void arc_mpu_init(void);
77 extern void arc_secureshield_init(void);
78 
79 /**
80  * @brief Prepare to and run C code
81  *
82  * This routine prepares for the execution of and runs C code.
83  */
84 
z_prep_c(void)85 FUNC_NORETURN void z_prep_c(void)
86 {
87 	soc_prep_hook();
88 
89 #ifdef CONFIG_ISA_ARCV3
90 	arc_cluster_scm_enable();
91 #endif
92 
93 	arch_bss_zero();
94 #ifdef __CCAC__
95 	dev_state_zero();
96 #endif
97 	arch_data_copy();
98 #if CONFIG_ARCH_CACHE
99 	arch_cache_init();
100 #endif
101 #ifdef CONFIG_ARC_MPU
102 	arc_mpu_init();
103 #endif
104 #ifdef CONFIG_ARC_SECURE_FIRMWARE
105 	arc_secureshield_init();
106 #endif
107 	z_cstart();
108 	CODE_UNREACHABLE;
109 }
110