time.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2009
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <time.h>
  10. #include <timer.h>
  11. #include <watchdog.h>
  12. #include <div64.h>
  13. #include <asm/io.h>
  14. #ifndef CONFIG_WD_PERIOD
  15. # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
  16. #endif
  17. DECLARE_GLOBAL_DATA_PTR;
  18. #ifdef CONFIG_SYS_TIMER_RATE
  19. /* Returns tick rate in ticks per second */
  20. ulong notrace get_tbclk(void)
  21. {
  22. return CONFIG_SYS_TIMER_RATE;
  23. }
  24. #endif
  25. #ifdef CONFIG_SYS_TIMER_COUNTER
  26. unsigned long notrace timer_read_counter(void)
  27. {
  28. #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
  29. return ~readl(CONFIG_SYS_TIMER_COUNTER);
  30. #else
  31. return readl(CONFIG_SYS_TIMER_COUNTER);
  32. #endif
  33. }
  34. ulong timer_get_boot_us(void)
  35. {
  36. ulong count = timer_read_counter();
  37. #if CONFIG_SYS_TIMER_RATE == 1000000
  38. return count;
  39. #elif CONFIG_SYS_TIMER_RATE > 1000000
  40. return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
  41. #elif defined(CONFIG_SYS_TIMER_RATE)
  42. return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
  43. #else
  44. /* Assume the counter is in microseconds */
  45. return count;
  46. #endif
  47. }
  48. #else
  49. extern unsigned long __weak timer_read_counter(void);
  50. #endif
  51. #if CONFIG_IS_ENABLED(TIMER)
  52. ulong notrace get_tbclk(void)
  53. {
  54. if (!gd->timer) {
  55. #ifdef CONFIG_TIMER_EARLY
  56. return timer_early_get_rate();
  57. #else
  58. int ret;
  59. ret = dm_timer_init();
  60. if (ret)
  61. return ret;
  62. #endif
  63. }
  64. return timer_get_rate(gd->timer);
  65. }
  66. uint64_t notrace get_ticks(void)
  67. {
  68. u64 count;
  69. int ret;
  70. if (!gd->timer) {
  71. #ifdef CONFIG_TIMER_EARLY
  72. return timer_early_get_count();
  73. #else
  74. int ret;
  75. ret = dm_timer_init();
  76. if (ret)
  77. return ret;
  78. #endif
  79. }
  80. ret = timer_get_count(gd->timer, &count);
  81. if (ret)
  82. return ret;
  83. return count;
  84. }
  85. #else /* !CONFIG_TIMER */
  86. uint64_t __weak notrace get_ticks(void)
  87. {
  88. unsigned long now = timer_read_counter();
  89. /* increment tbu if tbl has rolled over */
  90. if (now < gd->timebase_l)
  91. gd->timebase_h++;
  92. gd->timebase_l = now;
  93. return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
  94. }
  95. #endif /* CONFIG_TIMER */
  96. /* Returns time in milliseconds */
  97. static uint64_t notrace tick_to_time(uint64_t tick)
  98. {
  99. ulong div = get_tbclk();
  100. tick *= CONFIG_SYS_HZ;
  101. do_div(tick, div);
  102. return tick;
  103. }
  104. int __weak timer_init(void)
  105. {
  106. return 0;
  107. }
  108. /* Returns time in milliseconds */
  109. ulong __weak get_timer(ulong base)
  110. {
  111. return tick_to_time(get_ticks()) - base;
  112. }
  113. static uint64_t notrace tick_to_time_us(uint64_t tick)
  114. {
  115. ulong div = get_tbclk() / 1000;
  116. tick *= CONFIG_SYS_HZ;
  117. do_div(tick, div);
  118. return tick;
  119. }
  120. uint64_t __weak get_timer_us(uint64_t base)
  121. {
  122. return tick_to_time_us(get_ticks()) - base;
  123. }
  124. unsigned long __weak notrace timer_get_us(void)
  125. {
  126. return tick_to_time(get_ticks() * 1000);
  127. }
  128. uint64_t usec_to_tick(unsigned long usec)
  129. {
  130. uint64_t tick = usec;
  131. tick *= get_tbclk();
  132. do_div(tick, 1000000);
  133. return tick;
  134. }
  135. void __weak __udelay(unsigned long usec)
  136. {
  137. uint64_t tmp;
  138. tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
  139. while (get_ticks() < tmp+1) /* loop till event */
  140. /*NOP*/;
  141. }
  142. /* ------------------------------------------------------------------------- */
  143. void udelay(unsigned long usec)
  144. {
  145. ulong kv;
  146. do {
  147. WATCHDOG_RESET();
  148. kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
  149. __udelay (kv);
  150. usec -= kv;
  151. } while(usec);
  152. }