generic_timer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2013
  4. * David Feng <fenghua@phytium.com.cn>
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <asm/system.h>
  9. DECLARE_GLOBAL_DATA_PTR;
  10. /*
  11. * Generic timer implementation of get_tbclk()
  12. */
  13. unsigned long get_tbclk(void)
  14. {
  15. unsigned long cntfrq;
  16. asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq));
  17. return cntfrq;
  18. }
  19. #ifdef CONFIG_SYS_FSL_ERRATUM_A008585
  20. /*
  21. * FSL erratum A-008585 says that the ARM generic timer counter "has the
  22. * potential to contain an erroneous value for a small number of core
  23. * clock cycles every time the timer value changes".
  24. * This sometimes leads to a consecutive counter read returning a lower
  25. * value than the previous one, thus reporting the time to go backwards.
  26. * The workaround is to read the counter twice and only return when the value
  27. * was the same in both reads.
  28. * Assumes that the CPU runs in much higher frequency than the timer.
  29. */
  30. unsigned long timer_read_counter(void)
  31. {
  32. unsigned long cntpct;
  33. unsigned long temp;
  34. isb();
  35. asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
  36. asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
  37. while (temp != cntpct) {
  38. asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
  39. asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
  40. }
  41. return cntpct;
  42. }
  43. #elif CONFIG_SUNXI_A64_TIMER_ERRATUM
  44. /*
  45. * This erratum sometimes flips the lower 11 bits of the counter value
  46. * to all 0's or all 1's, leading to jumps forwards or backwards.
  47. * Backwards jumps might be interpreted all roll-overs and be treated as
  48. * huge jumps forward.
  49. * The workaround is to check whether the lower 11 bits of the counter are
  50. * all 0 or all 1, then discard this value and read again.
  51. * This occasionally discards valid values, but will catch all erroneous
  52. * reads and fixes the problem reliably. Also this mostly requires only a
  53. * single read, so does not have any significant overhead.
  54. * The algorithm was conceived by Samuel Holland.
  55. */
  56. unsigned long timer_read_counter(void)
  57. {
  58. unsigned long cntpct;
  59. isb();
  60. do {
  61. asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
  62. } while (((cntpct + 1) & GENMASK(10, 0)) <= 1);
  63. return cntpct;
  64. }
  65. #else
  66. /*
  67. * timer_read_counter() using the Arm Generic Timer (aka arch timer).
  68. */
  69. unsigned long timer_read_counter(void)
  70. {
  71. unsigned long cntpct;
  72. isb();
  73. asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
  74. return cntpct;
  75. }
  76. #endif
  77. uint64_t get_ticks(void)
  78. {
  79. unsigned long ticks = timer_read_counter();
  80. gd->arch.tbl = ticks;
  81. return ticks;
  82. }
  83. unsigned long usec2ticks(unsigned long usec)
  84. {
  85. ulong ticks;
  86. if (usec < 1000)
  87. ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
  88. else
  89. ticks = ((usec / 10) * (get_tbclk() / 100000));
  90. return ticks;
  91. }
  92. ulong timer_get_boot_us(void)
  93. {
  94. u64 val = get_ticks() * 1000000;
  95. return val / get_tbclk();
  96. }