1 /*
2  * Copyright (c) 2021-2023 HPMicro
3  *
4  *
5  */
6 
7 #include "hpm_common.h"
8 #include "hpm_soc.h"
9 #include "hpm_l1c_drv.h"
10 #include <rtthread.h>
11 
12 void system_init(void);
13 
14 extern int entry(void);
15 
16 extern void __libc_init_array(void);
17 extern void __libc_fini_array(void);
18 
system_init(void)19 void system_init(void)
20 {
21     disable_global_irq(CSR_MSTATUS_MIE_MASK);
22     disable_irq_from_intc();
23     enable_irq_from_intc();
24     enable_global_irq(CSR_MSTATUS_MIE_MASK);
25 #ifndef CONFIG_NOT_ENABLE_ICACHE
26     l1c_ic_enable();
27 #endif
28 #ifndef CONFIG_NOT_ENABLE_DCACHE
29     l1c_dc_enable();
30 #endif
31 }
32 
c_startup(void)33 __attribute__((weak)) void c_startup(void)
34 {
35     uint32_t i, size;
36 #ifdef FLASH_XIP
37     extern uint8_t __vector_ram_start__[], __vector_ram_end__[], __vector_load_addr__[];
38     size = __vector_ram_end__ - __vector_ram_start__;
39     for (i = 0; i < size; i++) {
40         *(__vector_ram_start__ + i) = *(__vector_load_addr__ + i);
41     }
42 #endif
43 
44     extern uint8_t __etext[];
45     extern uint8_t __bss_start__[], __bss_end__[];
46     extern uint8_t __tbss_start__[], __tbss_end__[];
47     extern uint8_t __tdata_start__[], __tdata_end__[];
48     extern uint8_t __data_start__[], __data_end__[];
49     extern uint8_t __noncacheable_bss_start__[], __noncacheable_bss_end__[];
50     extern uint8_t __ramfunc_start__[], __ramfunc_end__[];
51     extern uint8_t __noncacheable_init_start__[], __noncacheable_init_end__[];
52 
53     /* tbss section */
54     size = __tbss_end__ - __tbss_start__;
55     for (i = 0; i < size; i++) {
56         *(__tbss_start__ + i) = 0;
57     }
58 
59     /* bss section */
60     size = __bss_end__ - __bss_start__;
61     for (i = 0; i < size; i++) {
62         *(__bss_start__ + i) = 0;
63     }
64 
65     /* noncacheable bss section */
66     size = __noncacheable_bss_end__ - __noncacheable_bss_start__;
67     for (i = 0; i < size; i++) {
68         *(__noncacheable_bss_start__ + i) = 0;
69     }
70 
71     /* tdata section LMA: etext */
72     size = __tdata_end__ - __tdata_start__;
73     for (i = 0; i < size; i++) {
74         *(__tdata_start__ + i) = *(__etext + i);
75     }
76 
77     /* data section LMA: etext */
78     size = __data_end__ - __data_start__;
79     for (i = 0; i < size; i++) {
80         *(__data_start__ + i) = *(__etext + (__tdata_end__ - __tdata_start__) + i);
81     }
82 
83     /* ramfunc section LMA: etext + data length */
84     size = __ramfunc_end__ - __ramfunc_start__;
85     for (i = 0; i < size; i++) {
86         *(__ramfunc_start__ + i) = *(__etext + (__data_end__ - __tdata_start__) + i);
87     }
88 
89     /* noncacheable init section LMA: etext + data length + ramfunc length */
90     size = __noncacheable_init_end__ - __noncacheable_init_start__;
91     for (i = 0; i < size; i++) {
92         *(__noncacheable_init_start__ + i) = *(__etext + (__data_end__ - __tdata_start__) + (__ramfunc_end__ - __ramfunc_start__) + i);
93     }
94 }
95 
main(void)96 __attribute__((weak)) int main(void)
97 {
98     while(1);
99 }
100 
reset_handler(void)101 void reset_handler(void)
102 {
103     /**
104      * Disable preemptive interrupt
105      */
106     HPM_PLIC->FEATURE = 0;
107     /*
108      * Initialize LMA/VMA sections.
109      * Relocation for any sections that need to be copied from LMA to VMA.
110      */
111     c_startup();
112 
113     /* Call platform specific hardware initialization */
114     system_init();
115 
116     /* Do global constructors */
117     __libc_init_array();
118 
119 
120 
121     /* Entry function */
122     entry();
123 }
124 
125 
_init()126 __attribute__((weak)) void _init()
127 {
128 }
129