time.c 3.3 KB

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