1 /*
2  * Copyright (c) 2006-2020, 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 "mm_aspace.h"
17 #include "tick.h"
18 
19 #include "drv_uart.h"
20 #include "encoding.h"
21 #include "stack.h"
22 #include "sbi.h"
23 #include "riscv.h"
24 #include "plic.h"
25 #include "stack.h"
26 
27 #ifdef RT_USING_SMART
28 #include "riscv_mmu.h"
29 #include "mmu.h"
30 #include "page.h"
31 #include "lwp_arch.h"
32 
33 rt_region_t init_page_region = {(rt_size_t)RT_HW_PAGE_START, (rt_size_t)RT_HW_PAGE_END};
34 
35 extern size_t MMUTable[];
36 
37 struct mem_desc platform_mem_desc[] = {
38     {KERNEL_VADDR_START, (rt_size_t)RT_HW_PAGE_END - 1, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM},
39 };
40 
41 #define NUM_MEM_DESC (sizeof(platform_mem_desc) / sizeof(platform_mem_desc[0]))
42 
43 #endif
44 
primary_cpu_entry(void)45 void primary_cpu_entry(void)
46 {
47     /* disable global interrupt */
48     rt_hw_interrupt_disable();
49 
50     entry();
51 }
52 
53 #define IOREMAP_SIZE (1ul << 30)
54 
55 #ifndef ARCH_REMAP_KERNEL
56 #define IOREMAP_VEND USER_VADDR_START
57 #else
58 #define IOREMAP_VEND 0ul
59 #endif /* ARCH_REMAP_KERNEL */
60 
rt_hw_board_init(void)61 void rt_hw_board_init(void)
62 {
63 #ifdef RT_USING_SMART
64     /* init data structure */
65     rt_hw_mmu_map_init(&rt_kernel_space, (void *)(IOREMAP_VEND - IOREMAP_SIZE), IOREMAP_SIZE, (rt_size_t *)MMUTable, PV_OFFSET);
66 
67     /* init page allocator */
68     rt_page_init(init_page_region);
69 
70     /* setup region, and enable MMU */
71     rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, NUM_MEM_DESC);
72 #endif
73 
74 #ifdef RT_USING_HEAP
75     /* initialize memory system */
76     rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
77 #endif
78 
79     plic_init();
80 
81     rt_hw_interrupt_init();
82 
83     rt_hw_uart_init();
84 
85 #ifdef RT_USING_CONSOLE
86     /* set console device */
87     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
88 #endif /* RT_USING_CONSOLE */
89 
90     rt_hw_tick_init();
91 
92 #ifdef RT_USING_COMPONENTS_INIT
93     rt_components_board_init();
94 #endif
95 
96 #ifdef RT_USING_HEAP
97     rt_kprintf("heap: [0x%08x - 0x%08x]\n", (rt_ubase_t)RT_HW_HEAP_BEGIN, (rt_ubase_t)RT_HW_HEAP_END);
98 #endif /* RT_USING_HEAP */
99 }
100 
rt_hw_cpu_reset(void)101 void rt_hw_cpu_reset(void)
102 {
103     sbi_shutdown();
104 
105     while (1)
106         ;
107 }
108 MSH_CMD_EXPORT_ALIAS(rt_hw_cpu_reset, reboot, reset machine);
109 
110