10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci *
50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
60d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
70d163575Sopenharmony_ci *
80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
90d163575Sopenharmony_ci *    conditions and the following disclaimer.
100d163575Sopenharmony_ci *
110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
120d163575Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
130d163575Sopenharmony_ci *    provided with the distribution.
140d163575Sopenharmony_ci *
150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
160d163575Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
170d163575Sopenharmony_ci *    permission.
180d163575Sopenharmony_ci *
190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300d163575Sopenharmony_ci */
310d163575Sopenharmony_ci
320d163575Sopenharmony_ci#include "los_hw_pri.h"
330d163575Sopenharmony_ci#include "los_tick_pri.h"
340d163575Sopenharmony_ci#include "los_sys_pri.h"
350d163575Sopenharmony_ci#include "gic_common.h"
360d163575Sopenharmony_ci
370d163575Sopenharmony_ci#define STRING_COMB(x, y, z)        x ## y ## z
380d163575Sopenharmony_ci
390d163575Sopenharmony_ci#ifdef LOSCFG_ARCH_SECURE_MONITOR_MODE
400d163575Sopenharmony_ci#define TIMER_REG(reg)              STRING_COMB(TIMER_REG_, CNTPS, reg)
410d163575Sopenharmony_ci#else
420d163575Sopenharmony_ci#define TIMER_REG(reg)              STRING_COMB(TIMER_REG_, CNTP, reg)
430d163575Sopenharmony_ci#endif
440d163575Sopenharmony_ci#define TIMER_REG_CTL               TIMER_REG(_CTL)     /* 32 bits */
450d163575Sopenharmony_ci#define TIMER_REG_TVAL              TIMER_REG(_TVAL)    /* 32 bits */
460d163575Sopenharmony_ci#define TIMER_REG_CVAL              TIMER_REG(_CVAL)    /* 64 bits */
470d163575Sopenharmony_ci#define TIMER_REG_CT                TIMER_REG(CT)       /* 64 bits */
480d163575Sopenharmony_ci
490d163575Sopenharmony_ci#ifdef __LP64__
500d163575Sopenharmony_ci
510d163575Sopenharmony_ci#define TIMER_REG_CNTFRQ            cntfrq_el0
520d163575Sopenharmony_ci
530d163575Sopenharmony_ci/* CNTP AArch64 registers */
540d163575Sopenharmony_ci#define TIMER_REG_CNTP_CTL          cntp_ctl_el0
550d163575Sopenharmony_ci#define TIMER_REG_CNTP_TVAL         cntp_tval_el0
560d163575Sopenharmony_ci#define TIMER_REG_CNTP_CVAL         cntp_cval_el0
570d163575Sopenharmony_ci#define TIMER_REG_CNTPCT            cntpct_el0
580d163575Sopenharmony_ci
590d163575Sopenharmony_ci/* CNTPS AArch64 registers */
600d163575Sopenharmony_ci#define TIMER_REG_CNTPS_CTL         cntps_ctl_el1
610d163575Sopenharmony_ci#define TIMER_REG_CNTPS_TVAL        cntps_tval_el1
620d163575Sopenharmony_ci#define TIMER_REG_CNTPS_CVAL        cntps_cval_el1
630d163575Sopenharmony_ci#define TIMER_REG_CNTPSCT           cntpct_el0
640d163575Sopenharmony_ci
650d163575Sopenharmony_ci#define READ_TIMER_REG32(reg)       AARCH64_SYSREG_READ(reg)
660d163575Sopenharmony_ci#define READ_TIMER_REG64(reg)       AARCH64_SYSREG_READ(reg)
670d163575Sopenharmony_ci#define WRITE_TIMER_REG32(reg, val) AARCH64_SYSREG_WRITE(reg, (UINT64)(val))
680d163575Sopenharmony_ci#define WRITE_TIMER_REG64(reg, val) AARCH64_SYSREG_WRITE(reg, val)
690d163575Sopenharmony_ci
700d163575Sopenharmony_ci#else /* Aarch32 */
710d163575Sopenharmony_ci
720d163575Sopenharmony_ci#define TIMER_REG_CNTFRQ            CP15_REG(c14, 0, c0, 0)
730d163575Sopenharmony_ci
740d163575Sopenharmony_ci/* CNTP AArch32 registers */
750d163575Sopenharmony_ci#define TIMER_REG_CNTP_CTL          CP15_REG(c14, 0, c2, 1)
760d163575Sopenharmony_ci#define TIMER_REG_CNTP_TVAL         CP15_REG(c14, 0, c2, 0)
770d163575Sopenharmony_ci#define TIMER_REG_CNTP_CVAL         CP15_REG64(c14, 2)
780d163575Sopenharmony_ci#define TIMER_REG_CNTPCT            CP15_REG64(c14, 0)
790d163575Sopenharmony_ci
800d163575Sopenharmony_ci/* CNTPS AArch32 registers are banked and accessed though CNTP */
810d163575Sopenharmony_ci#define CNTPS CNTP
820d163575Sopenharmony_ci
830d163575Sopenharmony_ci#define READ_TIMER_REG32(reg)       ARM_SYSREG_READ(reg)
840d163575Sopenharmony_ci#define READ_TIMER_REG64(reg)       ARM_SYSREG64_READ(reg)
850d163575Sopenharmony_ci#define WRITE_TIMER_REG32(reg, val) ARM_SYSREG_WRITE(reg, val)
860d163575Sopenharmony_ci#define WRITE_TIMER_REG64(reg, val) ARM_SYSREG64_WRITE(reg, val)
870d163575Sopenharmony_ci
880d163575Sopenharmony_ci#endif
890d163575Sopenharmony_ci
900d163575Sopenharmony_ciUINT32 HalClockFreqRead(VOID)
910d163575Sopenharmony_ci{
920d163575Sopenharmony_ci    return READ_TIMER_REG32(TIMER_REG_CNTFRQ);
930d163575Sopenharmony_ci}
940d163575Sopenharmony_ci
950d163575Sopenharmony_ciVOID HalClockFreqWrite(UINT32 freq)
960d163575Sopenharmony_ci{
970d163575Sopenharmony_ci    WRITE_TIMER_REG32(TIMER_REG_CNTFRQ, freq);
980d163575Sopenharmony_ci}
990d163575Sopenharmony_ci
1000d163575Sopenharmony_ciSTATIC_INLINE VOID TimerCtlWrite(UINT32 cntpCtl)
1010d163575Sopenharmony_ci{
1020d163575Sopenharmony_ci    WRITE_TIMER_REG32(TIMER_REG_CTL, cntpCtl);
1030d163575Sopenharmony_ci}
1040d163575Sopenharmony_ci
1050d163575Sopenharmony_ciSTATIC_INLINE UINT64 TimerCvalRead(VOID)
1060d163575Sopenharmony_ci{
1070d163575Sopenharmony_ci    return READ_TIMER_REG64(TIMER_REG_CVAL);
1080d163575Sopenharmony_ci}
1090d163575Sopenharmony_ci
1100d163575Sopenharmony_ciSTATIC_INLINE VOID TimerCvalWrite(UINT64 cval)
1110d163575Sopenharmony_ci{
1120d163575Sopenharmony_ci    WRITE_TIMER_REG64(TIMER_REG_CVAL, cval);
1130d163575Sopenharmony_ci}
1140d163575Sopenharmony_ci
1150d163575Sopenharmony_ciSTATIC_INLINE VOID TimerTvalWrite(UINT32 tval)
1160d163575Sopenharmony_ci{
1170d163575Sopenharmony_ci    WRITE_TIMER_REG32(TIMER_REG_TVAL, tval);
1180d163575Sopenharmony_ci}
1190d163575Sopenharmony_ci
1200d163575Sopenharmony_ciUINT64 HalClockGetCycles(VOID)
1210d163575Sopenharmony_ci{
1220d163575Sopenharmony_ci    UINT64 cntpct;
1230d163575Sopenharmony_ci
1240d163575Sopenharmony_ci    cntpct = READ_TIMER_REG64(TIMER_REG_CT);
1250d163575Sopenharmony_ci    return cntpct;
1260d163575Sopenharmony_ci}
1270d163575Sopenharmony_ci
1280d163575Sopenharmony_ciLITE_OS_SEC_TEXT_INIT VOID HalClockInit(VOID)
1290d163575Sopenharmony_ci{
1300d163575Sopenharmony_ci    UINT32 ret;
1310d163575Sopenharmony_ci
1320d163575Sopenharmony_ci    g_sysClock = HalClockFreqRead();
1330d163575Sopenharmony_ci    ret = LOS_HwiCreate(OS_TICK_INT_NUM, MIN_INTERRUPT_PRIORITY, 0, OsTickHandler, 0);
1340d163575Sopenharmony_ci    if (ret != LOS_OK) {
1350d163575Sopenharmony_ci        PRINT_ERR("%s, %d create tick irq failed, ret:0x%x\n", __FUNCTION__, __LINE__, ret);
1360d163575Sopenharmony_ci    }
1370d163575Sopenharmony_ci}
1380d163575Sopenharmony_ci
1390d163575Sopenharmony_ciLITE_OS_SEC_TEXT_INIT VOID HalClockStart(VOID)
1400d163575Sopenharmony_ci{
1410d163575Sopenharmony_ci    HalIrqUnmask(OS_TICK_INT_NUM);
1420d163575Sopenharmony_ci
1430d163575Sopenharmony_ci    /* triggle the first tick */
1440d163575Sopenharmony_ci    TimerCtlWrite(0);
1450d163575Sopenharmony_ci    TimerTvalWrite(OS_CYCLE_PER_TICK);
1460d163575Sopenharmony_ci    TimerCtlWrite(1);
1470d163575Sopenharmony_ci}
1480d163575Sopenharmony_ci
1490d163575Sopenharmony_ciVOID HalDelayUs(UINT32 usecs)
1500d163575Sopenharmony_ci{
1510d163575Sopenharmony_ci    UINT64 cycles = (UINT64)usecs * g_sysClock / OS_SYS_US_PER_SECOND;
1520d163575Sopenharmony_ci    UINT64 deadline = HalClockGetCycles() + cycles;
1530d163575Sopenharmony_ci
1540d163575Sopenharmony_ci    while (HalClockGetCycles() < deadline) {
1550d163575Sopenharmony_ci        __asm__ volatile ("nop");
1560d163575Sopenharmony_ci    }
1570d163575Sopenharmony_ci}
1580d163575Sopenharmony_ci
1590d163575Sopenharmony_ciDEPRECATED UINT64 hi_sched_clock(VOID)
1600d163575Sopenharmony_ci{
1610d163575Sopenharmony_ci    return LOS_CurrNanosec();
1620d163575Sopenharmony_ci}
1630d163575Sopenharmony_ci
1640d163575Sopenharmony_ciUINT32 HalClockGetTickTimerCycles(VOID)
1650d163575Sopenharmony_ci{
1660d163575Sopenharmony_ci    UINT64 cval = TimerCvalRead();
1670d163575Sopenharmony_ci    UINT64 cycles = HalClockGetCycles();
1680d163575Sopenharmony_ci
1690d163575Sopenharmony_ci    return (UINT32)((cval > cycles) ? (cval - cycles) : 0);
1700d163575Sopenharmony_ci}
1710d163575Sopenharmony_ci
1720d163575Sopenharmony_ciUINT64 HalClockTickTimerReload(UINT64 cycles)
1730d163575Sopenharmony_ci{
1740d163575Sopenharmony_ci    HalIrqMask(OS_TICK_INT_NUM);
1750d163575Sopenharmony_ci    HalIrqClear(OS_TICK_INT_NUM);
1760d163575Sopenharmony_ci
1770d163575Sopenharmony_ci    TimerCtlWrite(0);
1780d163575Sopenharmony_ci    TimerCvalWrite(HalClockGetCycles() + cycles);
1790d163575Sopenharmony_ci    TimerCtlWrite(1);
1800d163575Sopenharmony_ci
1810d163575Sopenharmony_ci    HalIrqUnmask(OS_TICK_INT_NUM);
1820d163575Sopenharmony_ci    return cycles;
1830d163575Sopenharmony_ci}
184