fgeneric_timer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: generic_timer.c
  15. * Date: 2022-02-10 14:53:41
  16. * LastEditTime: 2022-02-17 17:30:07
  17. * Description:  This files is for
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. */
  23. #include "fparameters.h"
  24. #include "fgeneric_timer.h"
  25. #include "faarch32.h"
  26. #include "sdkconfig.h"
  27. #ifndef SDK_CONFIG_H__
  28. #warning "Please include sdkconfig.h"
  29. #endif
  30. #ifdef CONFIG_USE_SYS_TICK
  31. #include "fassert.h"
  32. #include "finterrupt.h"
  33. static volatile u32 genericTick;
  34. static GenericTimerTickHandler usr_tick_handler = NULL;
  35. #endif
  36. #define AARCH32_CNTP_CTL_ENABLE_MASK (1ul << 0)
  37. #define AARCH32_CNTP_CTL_INTERRUPT_MASK (1ul << 1)
  38. void GenericTimerStart(void)
  39. {
  40. u32 ctrl = aarch32_cntp_ctl_get();
  41. if (!(ctrl & AARCH32_CNTP_CTL_ENABLE_MASK))
  42. {
  43. ctrl |= AARCH32_CNTP_CTL_ENABLE_MASK;
  44. aarch32_cntp_ctl_set(ctrl);
  45. }
  46. }
  47. void GenericTimerStop(void)
  48. {
  49. u32 ctrl = aarch32_cntp_ctl_get();
  50. if ((ctrl & AARCH32_CNTP_CTL_ENABLE_MASK))
  51. {
  52. ctrl &= ~AARCH32_CNTP_CTL_ENABLE_MASK;
  53. aarch32_cntp_ctl_set(ctrl);
  54. }
  55. }
  56. void GenericTimerInterruptEnable(void)
  57. {
  58. u32 ctrl = aarch32_cntp_ctl_get();
  59. if (ctrl & AARCH32_CNTP_CTL_INTERRUPT_MASK)
  60. {
  61. ctrl &= ~AARCH32_CNTP_CTL_INTERRUPT_MASK;
  62. aarch32_cntp_ctl_set(ctrl);
  63. }
  64. }
  65. void GenericTimerInterruptDisable(void)
  66. {
  67. u64 ctrl = aarch32_cntp_ctl_get();
  68. if (!(ctrl & AARCH32_CNTP_CTL_INTERRUPT_MASK))
  69. {
  70. ctrl |= AARCH32_CNTP_CTL_INTERRUPT_MASK;
  71. aarch32_cntp_ctl_set(ctrl);
  72. }
  73. }
  74. u32 GenericTimerFrequecy(void)
  75. {
  76. u32 rate = aarch32_cntfrq_get();
  77. return (rate != 0) ? rate : 1000000;
  78. }
  79. u64 GenericTimerRead(void)
  80. {
  81. return aarch32_cntpct_get();
  82. }
  83. void GenericTimerCompare(u32 interval)
  84. {
  85. aarch32_cntp_tval_set(interval);
  86. }
  87. #ifdef CONFIG_USE_SYS_TICK
  88. static void GenericTimerClearTickIntr(u32 tickRateHz)
  89. {
  90. GenericTimerCompare(GenericTimerFrequecy() / tickRateHz);
  91. }
  92. static void GenericTimerTickIntrHandler(s32 vector, void *param)
  93. {
  94. u32 tickRateHz = (u32)param;
  95. (void)vector;
  96. genericTick++; /* tick */
  97. GenericTimerClearTickIntr(tickRateHz); /* clear tick intrrupt */
  98. if (usr_tick_handler) /* execute user handler */
  99. usr_tick_handler();
  100. }
  101. #endif
  102. void GenericTimerSetupSystick(u32 tickRateHz, GenericTimerTickHandler tickHandler, u32 intrPrority)
  103. {
  104. #ifdef CONFIG_USE_SYS_TICK
  105. u32 cntFrq;
  106. /* disable timer and get system frequency */
  107. GenericTimerStop();
  108. cntFrq = GenericTimerFrequecy();
  109. /* set tick rate */
  110. GenericTimerCompare(cntFrq / tickRateHz);
  111. GenericTimerInterruptEnable();
  112. /* set generic timer intrrupt */
  113. InterruptSetPriority(GENERIC_TIMER_NS_IRQ_NUM, intrPrority);
  114. /* install tick handler */
  115. usr_tick_handler = tickHandler;
  116. InterruptInstall(GENERIC_TIMER_NS_IRQ_NUM, GenericTimerTickIntrHandler,
  117. (void *)tickRateHz, "GenericTimerTick");
  118. /* enable intrrupt */
  119. InterruptUmask(GENERIC_TIMER_NS_IRQ_NUM);
  120. GenericTimerStart();
  121. #endif
  122. }
  123. u32 GenericGetTick(void)
  124. {
  125. #ifdef CONFIG_USE_SYS_TICK
  126. return genericTick;
  127. #else
  128. return 0xffU;
  129. #endif
  130. }