time.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. int ret;
  41. ret = dm_timer_init();
  42. if (ret)
  43. return ret;
  44. return timer_get_rate(gd->timer);
  45. }
  46. uint64_t notrace get_ticks(void)
  47. {
  48. u64 count;
  49. int ret;
  50. ret = dm_timer_init();
  51. if (ret)
  52. return ret;
  53. ret = timer_get_count(gd->timer, &count);
  54. if (ret)
  55. return ret;
  56. return count;
  57. }
  58. #else /* !CONFIG_TIMER */
  59. uint64_t __weak notrace get_ticks(void)
  60. {
  61. unsigned long now = timer_read_counter();
  62. /* increment tbu if tbl has rolled over */
  63. if (now < gd->timebase_l)
  64. gd->timebase_h++;
  65. gd->timebase_l = now;
  66. return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
  67. }
  68. #endif /* CONFIG_TIMER */
  69. /* Returns time in milliseconds */
  70. static uint64_t notrace tick_to_time(uint64_t tick)
  71. {
  72. ulong div = get_tbclk();
  73. tick *= CONFIG_SYS_HZ;
  74. do_div(tick, div);
  75. return tick;
  76. }
  77. int __weak timer_init(void)
  78. {
  79. return 0;
  80. }
  81. /* Returns time in milliseconds */
  82. ulong __weak get_timer(ulong base)
  83. {
  84. return tick_to_time(get_ticks()) - base;
  85. }
  86. unsigned long __weak notrace timer_get_us(void)
  87. {
  88. return tick_to_time(get_ticks() * 1000);
  89. }
  90. static uint64_t usec_to_tick(unsigned long usec)
  91. {
  92. uint64_t tick = usec;
  93. tick *= get_tbclk();
  94. do_div(tick, 1000000);
  95. return tick;
  96. }
  97. void __weak __udelay(unsigned long usec)
  98. {
  99. uint64_t tmp;
  100. tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
  101. while (get_ticks() < tmp+1) /* loop till event */
  102. /*NOP*/;
  103. }
  104. /* ------------------------------------------------------------------------- */
  105. void udelay(unsigned long usec)
  106. {
  107. ulong kv;
  108. do {
  109. WATCHDOG_RESET();
  110. kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
  111. __udelay (kv);
  112. usec -= kv;
  113. } while(usec);
  114. }
  115. void mdelay(unsigned long msec)
  116. {
  117. while (msec--)
  118. udelay(1000);
  119. }