time.c 3.3 KB

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