time.c 3.7 KB

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