time.c 3.5 KB

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