1 /*
2  * File      : board.c
3  * This file is part of RT-Thread RTOS
4  * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License along
17  *  with this program; if not, write to the Free Software Foundation, Inc.,
18  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Change Logs:
21  * Date           Author       Notes
22  */
23 
24 #include <interrupt.h>
25 #include <rthw.h>
26 
27 #include <board.h>
28 #include <platform.h>
29 #include <encoding.h>
30 #include <interrupt.h>
31 
32 extern void use_default_clocks(void);
33 extern void use_pll(int refsel, int bypass, int r, int f, int q);
34 
35 #define TICK_COUNT  (RTC_FREQ / RT_TICK_PER_SECOND)
36 
37 #define MTIME       (*((volatile uint64_t *)(CLINT_CTRL_ADDR + CLINT_MTIME)))
38 #define MTIMECMP    (*((volatile uint64_t *)(CLINT_CTRL_ADDR + CLINT_MTIMECMP)))
39 
40 /* system tick interrupt */
handle_m_time_interrupt()41 void handle_m_time_interrupt()
42 {
43     MTIMECMP = MTIME + TICK_COUNT;
44     rt_tick_increase();
45 }
46 
47 /* fixed misaligned bug for qemu */
__wrap_memset(void * s,int c,size_t n)48 void *__wrap_memset(void *s, int c, size_t n)
49 {
50     return rt_memset(s, c, n);
51 }
52 
rt_hw_clock_init(void)53 static void rt_hw_clock_init(void)
54 {
55     use_default_clocks();
56     use_pll(0, 0, 1, 31, 1);
57 }
58 
rt_hw_timer_init(void)59 static void rt_hw_timer_init(void)
60 {
61     MTIMECMP = MTIME + TICK_COUNT;
62 
63     /*  enable timer interrupt*/
64     set_csr(mie, MIP_MTIP);
65 }
66 
rt_hw_board_init(void)67 void rt_hw_board_init(void)
68 {
69     /* initialize the system clock */
70     rt_hw_clock_init();
71 
72     /* initialize hardware interrupt */
73     rt_hw_interrupt_init();
74 
75     /* initialize timer0 */
76     rt_hw_timer_init();
77 
78 #ifdef RT_USING_HEAP
79     rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
80 #endif
81 
82 #ifdef RT_USING_COMPONENTS_INIT
83     rt_components_board_init();
84 #endif
85 
86 #ifdef RT_USING_CONSOLE
87     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
88 #endif
89 
90     return;
91 }
92 
93