1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "los_timer.h"
33 #include "los_config.h"
34 #include "los_tick.h"
35 #include "los_arch_interrupt.h"
36 #include "los_arch_timer.h"
37 #include "los_debug.h"
38
39 STATIC UINT32 SysTickStart(HWI_PROC_FUNC handler);
40 STATIC UINT64 SysTickReload(UINT64 nextResponseTime);
41 STATIC UINT64 SysTickCycleGet(UINT32 *period);
42 STATIC VOID SysTickLock(VOID);
43 STATIC VOID SysTickUnlock(VOID);
44
45 STATIC ArchTickTimer g_archTickTimer = {
46 .freq = 0,
47 .irqNum = OS_TICK_INT_NUM,
48 .periodMax = LOSCFG_BASE_CORE_TICK_RESPONSE_MAX,
49 .init = SysTickStart,
50 .getCycle = SysTickCycleGet,
51 .reload = SysTickReload,
52 .lock = SysTickLock,
53 .unlock = SysTickUnlock,
54 .tickHandler = NULL,
55 };
56
SysTickStart(HWI_PROC_FUNC handler)57 STATIC UINT32 SysTickStart(HWI_PROC_FUNC handler)
58 {
59 ArchTickTimer *tick = &g_archTickTimer;
60
61 tick->freq = OS_SYS_CLOCK;
62
63 #if (LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT == 1)
64 #if (LOSCFG_PLATFORM_HWI_WITH_ARG == 1)
65 OsSetVector(tick->irqNum, handler, NULL);
66 #else
67 OsSetVector(tick->irqNum, handler);
68 #endif
69 #endif
70
71 ResetCcount();
72 SetCcompare(LOSCFG_BASE_CORE_TICK_RESPONSE_MAX);
73
74 __asm__ __volatile__("wsr %0, ccompare1; rsync" : : "a"(0));
75 __asm__ __volatile__("wsr %0, ccompare2; rsync" : : "a"(0));
76
77 LOS_HwiEnable(tick->irqNum);
78 return LOS_OK;
79 }
80
SysTickReload(UINT64 nextResponseTime)81 STATIC UINT64 SysTickReload(UINT64 nextResponseTime)
82 {
83 UINT32 timerL;
84 if (nextResponseTime > g_archTickTimer.periodMax) {
85 nextResponseTime = g_archTickTimer.periodMax;
86 }
87
88 timerL = GetCcount();
89 timerL += nextResponseTime;
90 SetCcompare(timerL);
91 return nextResponseTime;
92 }
93
SysTickCycleGet(UINT32 * period)94 STATIC UINT64 SysTickCycleGet(UINT32 *period)
95 {
96 UINT32 tickCycleH;
97 UINT32 tickCycleL;
98 UINT32 temp;
99 static UINT64 tickCycle = 0;
100
101 (VOID)period;
102 UINT32 intSave = LOS_IntLock();
103 temp = tickCycle & 0xFFFFFFFF;
104 tickCycleH = tickCycle >> SHIFT_32_BIT;
105 tickCycleL = GetCcount();
106 if (tickCycleL < temp) {
107 tickCycleH++;
108 }
109 tickCycle = (((UINT64)tickCycleH) << SHIFT_32_BIT) | tickCycleL;
110
111 LOS_IntRestore(intSave);
112
113 return tickCycle;
114 }
115
SysTickLock(VOID)116 STATIC VOID SysTickLock(VOID)
117 {
118 LOS_HwiDisable(OS_TICK_INT_NUM);
119 }
120
SysTickUnlock(VOID)121 STATIC VOID SysTickUnlock(VOID)
122 {
123 LOS_HwiEnable(OS_TICK_INT_NUM);
124 }
125
ArchSysTickTimerGet(VOID)126 ArchTickTimer *ArchSysTickTimerGet(VOID)
127 {
128 return &g_archTickTimer;
129 }
130
Wfi(VOID)131 VOID Wfi(VOID)
132 {
133 __asm__ volatile("waiti 0" : : : "memory");
134 }
135
Dsb(VOID)136 VOID Dsb(VOID)
137 {
138 __asm__ volatile("dsync" : : : "memory");
139 }
140
ArchEnterSleep(VOID)141 UINT32 ArchEnterSleep(VOID)
142 {
143 Dsb();
144 Wfi();
145
146 return LOS_OK;
147 }
148