time.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 CFG_WD_PERIOD
  21. # define CFG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
  22. #endif
  23. DECLARE_GLOBAL_DATA_PTR;
  24. #ifdef CFG_SYS_TIMER_RATE
  25. /* Returns tick rate in ticks per second */
  26. ulong notrace get_tbclk(void)
  27. {
  28. return CFG_SYS_TIMER_RATE;
  29. }
  30. #endif
  31. #ifdef CFG_SYS_TIMER_COUNTER
  32. unsigned long notrace timer_read_counter(void)
  33. {
  34. #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
  35. return ~readl(CFG_SYS_TIMER_COUNTER);
  36. #else
  37. return readl(CFG_SYS_TIMER_COUNTER);
  38. #endif
  39. }
  40. ulong timer_get_boot_us(void)
  41. {
  42. ulong count = timer_read_counter();
  43. #ifdef CFG_SYS_TIMER_RATE
  44. const ulong timer_rate = CFG_SYS_TIMER_RATE;
  45. if (timer_rate == 1000000)
  46. return count;
  47. else if (timer_rate > 1000000)
  48. return lldiv(count, timer_rate / 1000000);
  49. else
  50. return (unsigned long long)count * 1000000 / timer_rate;
  51. #else
  52. /* Assume the counter is in microseconds */
  53. return count;
  54. #endif
  55. }
  56. #else
  57. extern unsigned long timer_read_counter(void);
  58. #endif
  59. #if CONFIG_IS_ENABLED(TIMER)
  60. ulong notrace get_tbclk(void)
  61. {
  62. if (!gd->timer) {
  63. int ret;
  64. if (IS_ENABLED(CONFIG_TIMER_EARLY))
  65. return timer_early_get_rate();
  66. ret = dm_timer_init();
  67. if (ret)
  68. return ret;
  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. int ret;
  78. if (IS_ENABLED(CONFIG_TIMER_EARLY))
  79. return timer_early_get_count();
  80. ret = dm_timer_init();
  81. if (ret)
  82. panic("Could not initialize timer (err %d)\n", ret);
  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. schedule();
  161. kv = usec > CFG_WD_PERIOD ? CFG_WD_PERIOD : usec;
  162. __udelay(kv);
  163. usec -= kv;
  164. } while(usec);
  165. }