1 /*
2 // Copyright (C) 2022 Beken Corporation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <common/bk_include.h>
17 #include "portmacro.h"
18 //#include "los_timer.h"
19 #include "bk_timer.h"
20 #include "los_reg.h"
21 //#include "los_debug.h"
22 //#include "los_timer.h"
23 #include "riscv_hal.h"
24 #undef REGBYTES
25 //#include "soc_common.h"
26 //#include "los_config.h"
27 #include "los_context.h"
28 #include <os/os.h>
29 #include "bk_arch.h"
30 #include "los_interrupt.h"
31 #include "los_tick.h"
32 #include <driver/timer.h>
33 #include <components/system.h>
34 #include "los_arch_interrupt.h"
35 #include "los_arch_timer.h"
36 #include "los_sched.h"
37
38 #include "platform.h"
39
40 #define ISR_STACK_SIZE 1024
41 #define BYTE_ALIGNMENT_MASK ( 0x000f )
42
43 static __attribute__ ((aligned(16))) StackType_t xISRStack[ ISR_STACK_SIZE ] = { 0 };
44 const StackType_t xISRStackTop = ( StackType_t ) &( xISRStack[ ISR_STACK_SIZE & ~BYTE_ALIGNMENT_MASK ] );
45
46
47 volatile uint64_t *mtime = ( volatile uint64_t * const )( MTIMER );
48 volatile uint64_t *mtimecmp = ( volatile uint64_t * const )( MTIMERCMP );
49
50 /* Used to program the machine timer compare register. */
51 uint64_t ullNextTime = 0ULL;
52 const uint64_t *pullNextTime = &ullNextTime;
53 const size_t uxTimerIncrementsForOneTick = ( size_t ) ( OS_SYS_CLOCK / LOSCFG_BASE_CORE_TICK_PER_SECOND ); /* Assumes increment won't go over 32-bits. */
54 uint32_t const ullMachineTimerCompareRegisterBase = MTIMERCMP;
55 volatile uint64_t * pullMachineTimerCompareRegister = NULL;
56
57 extern void mext_interrupt(void);
58
59 /*-----------------------------------------------------------*/
60
vPortSetupTimerInterrupt(void)61 void vPortSetupTimerInterrupt(void)
62 {
63 uint32_t ulCurrentTimeHigh, ulCurrentTimeLow;
64 volatile uint32_t * const pulTimeHigh = ( volatile uint32_t * const ) ( ( MTIMER ) + 4UL ); /* 8-byte typer so high 32-bit word is 4 bytes up. */
65 volatile uint32_t * const pulTimeLow = ( volatile uint32_t * const ) ( MTIMER );
66 volatile uint32_t ulHartId;
67
68 __asm volatile( "csrr %0, mhartid" : "=r"( ulHartId ) );
69 pullMachineTimerCompareRegister = ( volatile uint64_t * ) ( MTIMERCMP + ( ulHartId * sizeof( uint64_t ) ) );
70
71 do
72 {
73 ulCurrentTimeHigh = *pulTimeHigh;
74 ulCurrentTimeLow = *pulTimeLow;
75 } while( ulCurrentTimeHigh != *pulTimeHigh );
76
77 ullNextTime = ( uint64_t ) ulCurrentTimeHigh;
78 ullNextTime <<= 32ULL; /* High 4-byte word is 32-bits up. */
79 ullNextTime |= ( uint64_t ) ulCurrentTimeLow;
80 ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;
81 *pullMachineTimerCompareRegister = ullNextTime;
82
83 /* Prepare the time to use after the next tick interrupt. */
84 ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;
85
86 HalIrqEnable(RISCV_MACH_TIMER_IRQ);
87
88 }
89
vPortSetupMEXTInterrupt(void)90 void vPortSetupMEXTInterrupt(void)
91 {
92 UINT32 ret = -1;
93 UINT32 int_save;
94
95 int_save = LOS_IntLock();
96
97 ret = ArchHwiCreate(RISCV_MACH_EXT_IRQ, OS_HWI_PRIO_LOWEST, 0, (HWI_PROC_FUNC)mext_interrupt, 0);
98 LOS_IntRestore(int_save);
99 if (LOS_OK != ret) {
100 //PRINT_ERR("Setup MEXT Interrupt failed!\n");
101 return;
102 }
103
104 HalIrqEnable(RISCV_MACH_EXT_IRQ);
105 }
106
107 /*-----------------------------------------------------------*/
108
platform_is_in_interrupt_context(void)109 uint32_t platform_is_in_interrupt_context( void )
110 {
111 return ((uint32_t)ArchIsIntActive());
112 }
113 /*
114 * disable mie Interrupts
115 */
port_disable_mie_flag(void)116 unsigned int port_disable_mie_flag(void)
117 {
118 uint32_t uxSavedStatusValue;
119 uxSavedStatusValue = read_csr(NDS_MIE);
120 clear_csr(NDS_MIE, uxSavedStatusValue);
121
122 return uxSavedStatusValue;
123 }
124
125 /*
126 * Enable mie Interrupts
127 */
port_enable_mie_flag(uint32_t val)128 void port_enable_mie_flag(uint32_t val)
129 {
130 set_csr(NDS_MIE, val);
131 }
132
133