drv_timer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-22 Jesven first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <stdint.h>
  13. #include "mmu.h"
  14. #define TICK_PERIOD (g_sys_freq / RT_TICK_PER_SECOND)
  15. static int g_sys_freq;
  16. #define IRQ_SECURE_PHY_TIMER 29 /* Secure physical timer event */
  17. #define IRQ_NOSECURE_PHY_TIMER 30 /* No-Secure physical timer event */
  18. #define IRQ_SYS_TICK IRQ_SECURE_PHY_TIMER
  19. /* System Counter */
  20. struct sctr_regs {
  21. rt_uint32_t cntcr;
  22. rt_uint32_t cntsr;
  23. rt_uint32_t cntcv1;
  24. rt_uint32_t cntcv2;
  25. rt_uint32_t resv1[4];
  26. rt_uint32_t cntfid0;
  27. rt_uint32_t cntfid1;
  28. rt_uint32_t cntfid2;
  29. rt_uint32_t resv2[1001];
  30. rt_uint32_t counterid[1];
  31. };
  32. #define SC_CNTCR_ENABLE (1 << 0)
  33. #define SC_CNTCR_HDBG (1 << 1)
  34. #define SC_CNTCR_FREQ0 (1 << 8)
  35. #define SC_CNTCR_FREQ1 (1 << 9)
  36. #define isb() __asm__ __volatile__ ("" : : : "memory")
  37. #define dsb() __asm__ __volatile__ ("" : : : "memory")
  38. #define dmb() __asm__ __volatile__ ("" : : : "memory")
  39. static inline void enable_cntp(void)
  40. {
  41. rt_uint32_t cntv_ctl;
  42. cntv_ctl = 1;
  43. asm volatile ("mcr p15, 0, %0, c14, c2, 1" :: "r"(cntv_ctl)); // write CNTP_CTL
  44. isb();
  45. }
  46. static inline void disable_cntp(void)
  47. {
  48. rt_uint32_t cntv_ctl;
  49. cntv_ctl = 0;
  50. asm volatile ("mcr p15, 0, %0, c14, c2, 1" :: "r"(cntv_ctl)); // write CNTP_CTL
  51. isb();
  52. }
  53. static inline rt_uint32_t read_cntfrq(void)
  54. {
  55. rt_uint32_t val;
  56. asm volatile ("mrc p15, 0, %0, c14, c0, 0" : "=r"(val));
  57. return val;
  58. }
  59. static inline void write_cntp_tval(rt_uint32_t val)
  60. {
  61. asm volatile ("mcr p15, 0, %0, c14, c2, 0" :: "r"(val));
  62. isb();
  63. return;
  64. }
  65. static inline void write_cntp_cval(rt_uint64_t val)
  66. {
  67. asm volatile ("mcrr p15, 2, %Q0, %R0, c14" :: "r" (val));
  68. isb();
  69. return;
  70. }
  71. static inline rt_uint64_t read_cntp_cval(void)
  72. {
  73. rt_uint64_t val;
  74. asm volatile ("mrrc p15, 2, %Q0, %R0, c14" : "=r" (val));
  75. return (val);
  76. }
  77. volatile unsigned int *CCM_CLPCR;
  78. static void imx6ull_enable_clk_in_waitmode(void)
  79. {
  80. CCM_CLPCR = rt_ioremap((void*)0x20C4054, 4);
  81. *CCM_CLPCR &= ~((1 << 5) | 0x3);
  82. }
  83. static void system_counter_clk_source_init(void)
  84. {
  85. /* to do */
  86. }
  87. static void system_counter_init(void)
  88. {
  89. /* enable system_counter */
  90. #define SCTR_BASE_ADDR 0x021DC000
  91. #define CONFIG_SC_TIMER_CLK 8000000
  92. /* imx6ull, enable system counter */
  93. struct sctr_regs *sctr = (struct sctr_regs *)rt_ioremap((void*)SCTR_BASE_ADDR, sizeof(struct sctr_regs));
  94. unsigned long val, freq;
  95. freq = CONFIG_SC_TIMER_CLK;
  96. asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
  97. sctr->cntfid0 = freq;
  98. /* Enable system counter */
  99. val = sctr->cntcr;
  100. val &= ~(SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1);
  101. val |= SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG;
  102. sctr->cntcr = val;
  103. imx6ull_enable_clk_in_waitmode();
  104. }
  105. static void arch_timer_init(void)
  106. {
  107. g_sys_freq = read_cntfrq();
  108. /* set timeout val */
  109. disable_cntp();
  110. write_cntp_tval(TICK_PERIOD);
  111. /* start timer */
  112. enable_cntp();
  113. /* enable irq */
  114. }
  115. static void rt_hw_timer_isr(int vector, void *param)
  116. {
  117. rt_tick_increase();
  118. /* setup for next irq */
  119. /* clear interrupt */
  120. disable_cntp();
  121. write_cntp_cval(read_cntp_cval() + TICK_PERIOD);
  122. enable_cntp();
  123. }
  124. int rt_hw_timer_init(void)
  125. {
  126. /* Setup Timer for generating irq */
  127. /* enable timer */
  128. system_counter_clk_source_init();
  129. system_counter_init();
  130. arch_timer_init();
  131. /* insall irq, enable irq */
  132. rt_hw_interrupt_install(IRQ_SYS_TICK, rt_hw_timer_isr, RT_NULL, "tick");
  133. rt_hw_interrupt_umask(IRQ_SYS_TICK);
  134. return 0;
  135. }
  136. INIT_BOARD_EXPORT(rt_hw_timer_init);