time.c 3.0 KB

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