1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2021-01-30     lizhirui     first version
9  */
10 
11 #include <rthw.h>
12 #include <rtthread.h>
13 #include <rtdevice.h>
14 
15 #include "board.h"
16 #include "tick.h"
17 
18 #include "drv_uart.h"
19 
20 #include "sysctl_boot.h"
21 
22 #ifdef RT_USING_SMART
23 #include <mmu.h>
24 #include "page.h"
25 #endif
26 
27 extern unsigned int __sram_size;
28 extern unsigned int __sram_base;
29 extern unsigned int __sram_end;
30 #define RAM_END (rt_size_t)((void *)&__sram_end)
31 
32 extern unsigned int __bss_start;
33 extern unsigned int __bss_end;
34 
35 #define RT_HW_HEAP_BEGIN    ((void *)&__bss_end)
36 #define RT_HW_HEAP_END ((void *)(((rt_size_t)RT_HW_HEAP_BEGIN) + CONFIG_MEM_RTSMART_HEAP_SIZE))
37 
38 #define RT_HW_PAGE_START    ((void *)((rt_size_t)RT_HW_HEAP_END + sizeof(rt_size_t)))
39 #define RT_HW_PAGE_END      ((void *)(RAM_END))
40 
41 #ifdef RT_USING_SMART
42 
43 rt_region_t init_page_region = {(rt_size_t)RT_HW_PAGE_START, (rt_size_t)RT_HW_PAGE_END};
44 
45 extern size_t MMUTable[];
46 
47 struct mem_desc platform_mem_desc[] = {
48     {KERNEL_VADDR_START, (rt_size_t)(KERNEL_VADDR_START + CONFIG_MEM_MMZ_BASE + CONFIG_MEM_MMZ_SIZE - 1), (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM},
49 };
50 
51 #define NUM_MEM_DESC (sizeof(platform_mem_desc) / sizeof(platform_mem_desc[0]))
52 
53 #endif /* RT_USING_SMART */
54 
55 #ifndef ARCH_REMAP_KERNEL
56 #define IOREMAP_VEND USER_VADDR_START
57 #else
58 #define IOREMAP_VEND 0ul
59 #endif
60 
61 //初始化BSS节区
init_bss(void)62 void init_bss(void)
63 {
64     unsigned int *dst;
65 
66     dst = &__bss_start;
67     while ((rt_ubase_t)dst < (rt_ubase_t)&__bss_end)
68     {
69         *dst++ = 0;
70     }
71 }
72 
__rt_assert_handler(const char * ex_string,const char * func,rt_size_t line)73 static void __rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
74 {
75     rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
76     asm volatile("ebreak":::"memory");
77 }
78 
79 //BSP的C入口
primary_cpu_entry(void)80 void primary_cpu_entry(void)
81 {
82     //关中断
83     rt_hw_interrupt_disable();
84     rt_assert_set_hook(__rt_assert_handler);
85     //启动RT-Thread Smart内核
86     entry();
87 }
88 
89 #define IOREMAP_SIZE (1ul << 30)
90 
91 //这个初始化程序由内核主动调用,此时调度器还未启动,因此在此不能使用依赖线程上下文的函数
rt_hw_board_init(void)92 void rt_hw_board_init(void)
93 {
94 #ifdef RT_USING_SMART
95     /* init data structure */
96     rt_hw_mmu_map_init(&rt_kernel_space, (void *)(IOREMAP_VEND - IOREMAP_SIZE), IOREMAP_SIZE, (rt_size_t *)MMUTable, PV_OFFSET);
97 
98     /* init page allocator */
99     rt_page_init(init_page_region);
100 
101     /* setup region, and enable MMU */
102     rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, NUM_MEM_DESC);
103 #endif
104 
105 #ifdef RT_USING_HEAP
106     /* initialize memory system */
107     rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
108 #endif
109     /* initalize interrupt */
110     rt_hw_interrupt_init();
111 
112     /* initialize hardware interrupt */
113     rt_hw_uart_init();
114 
115     rt_hw_tick_init();
116 
117 #ifdef RT_USING_CONSOLE
118     /* set console device */
119     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
120 #endif /* RT_USING_CONSOLE */
121 
122 #ifdef RT_USING_COMPONENTS_INIT
123     rt_components_board_init();
124 #endif
125 }
126 
rt_hw_cpu_reset(void)127 void rt_hw_cpu_reset(void)
128 {
129     sysctl_boot_reset_soc();
130     while(1);
131 }
132 MSH_CMD_EXPORT_ALIAS(rt_hw_cpu_reset, reboot, reset machine);
133