time.c 3.6 KB

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