drv_timer.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2006-2026, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2026-04-20 rcitach first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <stdint.h>
  13. #include "board.h"
  14. #define S100_SYSCNT_BASE 0x23110000U
  15. #define S100_SYSCNT_CTRL_OSCON_MASK (0x1U)
  16. #define S100_SYSCNT_CLK_HZ (40000000U)
  17. #define S100_GENERIC_TIMER_PPI 30
  18. #define CNTP_CTL_ENABLE (1U << 0)
  19. struct s100_syscnt
  20. {
  21. volatile rt_uint32_t ctrl;
  22. volatile rt_uint32_t stat;
  23. volatile rt_uint32_t cntcvl;
  24. volatile rt_uint32_t cntcvu;
  25. rt_uint8_t reserved[16];
  26. volatile rt_uint32_t cntfid0;
  27. };
  28. static volatile struct s100_syscnt *const s100_syscnt =
  29. (volatile struct s100_syscnt *)S100_SYSCNT_BASE;
  30. static rt_uint32_t s100_tick_interval;
  31. static rt_uint64_t s100_read_cntpct(void)
  32. {
  33. rt_uint32_t low;
  34. rt_uint32_t high;
  35. __asm volatile ("mrrc p15, 0, %0, %1, c14" : "=r"(low), "=r"(high));
  36. return (((rt_uint64_t)high) << 32) | low;
  37. }
  38. static void s100_write_cntp_cval(rt_uint64_t value)
  39. {
  40. rt_uint32_t low = (rt_uint32_t)value;
  41. rt_uint32_t high = (rt_uint32_t)(value >> 32);
  42. __asm volatile ("mcrr p15, 2, %0, %1, c14" :: "r"(low), "r"(high));
  43. }
  44. static void s100_write_cntp_ctl(rt_uint32_t value)
  45. {
  46. __asm volatile ("mcr p15, 0, %0, c14, c2, 1" :: "r"(value));
  47. }
  48. static void s100_program_next_tick(rt_uint32_t delta)
  49. {
  50. s100_write_cntp_cval(s100_read_cntpct() + delta);
  51. }
  52. static void rt_hw_timer_isr(int vector, void *param)
  53. {
  54. RT_UNUSED(vector);
  55. RT_UNUSED(param);
  56. s100_program_next_tick(s100_tick_interval);
  57. rt_tick_increase();
  58. }
  59. int rt_hw_timer_init(void)
  60. {
  61. s100_tick_interval = S100_SYSCNT_CLK_HZ / RT_TICK_PER_SECOND;
  62. RT_ASSERT(s100_tick_interval > 0U);
  63. s100_syscnt->cntfid0 = S100_SYSCNT_CLK_HZ;
  64. s100_syscnt->ctrl |= S100_SYSCNT_CTRL_OSCON_MASK;
  65. s100_write_cntp_ctl(0U);
  66. s100_program_next_tick(s100_tick_interval);
  67. s100_write_cntp_ctl(CNTP_CTL_ENABLE);
  68. rt_hw_interrupt_install(S100_GENERIC_TIMER_PPI, rt_hw_timer_isr, RT_NULL, "tick");
  69. rt_hw_interrupt_umask(S100_GENERIC_TIMER_PPI);
  70. return 0;
  71. }
  72. INIT_BOARD_EXPORT(rt_hw_timer_init);