summaryrefslogtreecommitdiff
path: root/bsp/nuvoton/libraries/ma35/rtt_port/drv_common_aarch32.c
blob: 7dcce42c7363fdb00838f631ea70df03759629af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date            Author           Notes
* 2021-07-14      Wayne            First version
*
******************************************************************************/

#include <rtthread.h>

#if defined(USE_MA35D1_AARCH32)

#include <rthw.h>
#include <stdio.h>

#include "drv_common.h"
#include "board.h"
#include "drv_uart.h"
#include "drv_sspcc.h"
#include "drv_ssmcc.h"
#include "drv_umctl2.h"

#define LOG_TAG    "drv.common"
#define DBG_SECTION_NAME   LOG_TAG
#define DBG_LEVEL      LOG_LVL_INFO
#define DBG_COLOR
#include <rtdbg.h>

#define NORMAL_MEM_UNCACHED        (SHARED|AP_RW|DOMAIN0|STRONGORDER|DESC_SEC)

/*
MMU TLB setting:
0xFFFFFFFF  ----------------------------
            |  1GB DDR(non-cacheable)  |
0xC0000000  ----------------------------
            |    1GB DDR(cacheable)    |
0x80000000  ----------------------------
            |      DEVICE_MEM          |
            |                          |
0x00000000  ----------------------------
*/
struct mem_desc platform_mem_desc[] =
{
    {0x00000000,   0x7FFFFFFF, 0x00000000, DEVICE_MEM},          // Peripherals
    {0x80000000,   DDR_LIMIT_SIZE - 1, 0x80000000, NORMAL_MEM},  // 1GB DDR, cacheable
    {0xC0000000,   0xFFFFFFFF, 0x80000000, NORMAL_MEM_UNCACHED}  // 1GB DDR, non-cacheable
};
const rt_uint32_t platform_mem_desc_size = sizeof(platform_mem_desc) / sizeof(platform_mem_desc[0]);
/**
 * This function will initialize board
 */

struct rt_aspace mmu_info;

extern size_t MMUTable[];

extern void nu_clock_dump(void);
extern void nu_clock_raise(void);
extern void nu_clock_init(void);
extern void nu_chipcfg_dump(void);
extern uint32_t nu_chipcfg_ddrsize(void);

volatile uint32_t secondary_cpu_entry __attribute__((aligned(32))) = 0;
static rt_uint32_t timerStep;

void rt_hw_systick_isr(int vector, void *parameter)
{
    gtimer_set_load_value(timerStep);
    rt_tick_increase();
}

int rt_hw_systick_init(void)
{
    rt_hw_interrupt_install(NonSecPhysicalTimer_IRQn, rt_hw_systick_isr, RT_NULL, "systick");
    rt_hw_interrupt_umask(NonSecPhysicalTimer_IRQn);
    timerStep = gtimer_get_counter_frequency();
    timerStep /= RT_TICK_PER_SECOND;
    gtimer_set_load_value(timerStep);
    gtimer_set_control(1);
    return 0;
}

void rt_hw_us_delay(rt_uint32_t us)
{
    rt_uint32_t ticks;
    volatile rt_uint32_t told, tnow, tcnt = 0;
    rt_uint32_t cmp = timerStep;    // 12000 count / 1ms

    ticks = us * (cmp / 1000);      // us * 12(count/1us)
    told = gtimer_get_current_value();

    while (1)
    {
        /* Timer counter is increment. */
        tnow = gtimer_get_current_value();
        if (tnow != told)
        {
            /* 0 -- now === old -------- cmp */
            if (tnow < told)
            {
                tcnt += (told - tnow);
            }
            else
            {
                /* 0 == old --- new ======== cmp */
                tcnt += (cmp - tnow + told);
            }
            told = tnow;

            /* Timeout */
            if (tcnt >= ticks)
            {
                break;
            }
        }
        __NOP();
    }
} /* rt_hw_us_delay */

void idle_wfi(void)
{
    asm volatile("wfi");
}

rt_weak void nutool_pincfg_init(void)
{
}

/**
 * This function will initial board.
 */
rt_weak void rt_hw_board_init(void)
{
    uint32_t u32BoardHeapEnd;

    /* Unlock protected registers */
    SYS_UnlockReg();

    /* initialize SSPCC */
    nu_sspcc_init();

    /* initialize SSMCC */
    nu_ssmcc_init();

    /* initialize UMCTL2 */
    nu_umctl2_init();

    /* initialize base clock */
    nu_clock_init();

    /* initialize peripheral pin function */
    nutool_pincfg_init();

    rt_hw_mmu_map_init(&mmu_info, (void*)0x80000000, 0x10000000, MMUTable, 0);
    rt_hw_mmu_ioremap_init(&mmu_info, (void*)0x80000000, 0x10000000);

    /* initialize hardware interrupt */
    rt_hw_interrupt_init();

#if defined(RT_USING_HEAP)
    if (nu_chipcfg_ddrsize() > 0)
    {
        /* Get MCP DDR capacity in run-time. */
        u32BoardHeapEnd = 0x80000000 + nu_chipcfg_ddrsize();
    }
    else
    {
        /* Use board.h definition */
        u32BoardHeapEnd = (uint32_t)BOARD_HEAP_END;
    }
    rt_system_heap_init((void *)BOARD_HEAP_START, (void *)u32BoardHeapEnd);
#endif

    /* initialize uart */
    rt_hw_uart_init();

#if defined(RT_USING_CONSOLE)
    rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif

    nu_clock_raise();

    /* initialize systick */
    rt_hw_systick_init();
    rt_thread_idle_sethook(idle_wfi);

#if defined(RT_USING_COMPONENTS_INIT)
    rt_components_board_init();
#endif

#if defined(RT_USING_HEAP)
    /* Dump heap information */
    LOG_I("Heap: Begin@%08x, END@%08x, SIZE: %d MB", BOARD_HEAP_START, u32BoardHeapEnd, ((rt_uint32_t)u32BoardHeapEnd - (rt_uint32_t)BOARD_HEAP_START) / 1024 / 1024);
#endif

    nu_chipcfg_dump();
    nu_clock_dump();

#if defined(RT_USING_SMP)
    /* install IPI handle */
    rt_hw_interrupt_set_priority(RT_SCHEDULE_IPI, 16);
    rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler);
    rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
#endif
}

#if defined(RT_USING_SMP)

extern void rt_secondary_cpu_entry(void);
void set_secondary_cpu_boot_address(void)
{
    secondary_cpu_entry = (uint32_t)&rt_secondary_cpu_entry;
    rt_kprintf("Wake up cpu-1 goto -> 0x%08x@0x%08x\n", secondary_cpu_entry, &secondary_cpu_entry);
}

void rt_hw_secondary_cpu_up(void)
{
    rt_uint32_t i;
    rt_uint32_t cpu_mask;

    rt_kprintf("rt_hw_secondary_cpu_up is processing \r\n");

    set_secondary_cpu_boot_address();

    /* Flush to memory */
    rt_cpu_dcache_clean_flush();

    for (i = 1; i < RT_CPUS_NR; i++)
    {
        rt_kprintf("Bring up cpu-%d\r\n", i);
        cpu_mask = 1 << i;
        __asm__ volatile("dsb" ::
                         : "memory");
        __asm__ volatile("isb" ::
                         : "memory");
        __asm__ volatile("sev");
        rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
    }
}

void rt_hw_secondary_cpu_bsp_start(void)
{
    rt_kprintf("[%s] cpu-%d\r\n", __func__, rt_hw_cpu_id());

    rt_hw_vector_init();

    rt_hw_spin_lock(&_cpus_lock);

    arm_gic_cpu_init(0, platform_get_gic_cpu_base());
    rt_hw_systick_init();

    rt_system_scheduler_start();
}

void rt_hw_secondary_cpu_idle_exec(void)
{
    asm volatile("wfe" ::
                 : "memory", "cc");
}

#endif

#endif /* #if defined(USE_MA35D1_AARCH32) */