time.c 2.7 KB

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