time.c 3.7 KB

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