time.c 2.7 KB

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