time.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <watchdog.h>
  9. #include <div64.h>
  10. #include <asm/io.h>
  11. #if CONFIG_SYS_HZ != 1000
  12. #warning "CONFIG_SYS_HZ must be 1000 and should not be defined by platforms"
  13. #endif
  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. unsigned long long __weak notrace get_ticks(void)
  38. {
  39. unsigned long now = timer_read_counter();
  40. /* increment tbu if tbl has rolled over */
  41. if (now < gd->timebase_l)
  42. gd->timebase_h++;
  43. gd->timebase_l = now;
  44. return ((unsigned long long)gd->timebase_h << 32) | gd->timebase_l;
  45. }
  46. /* Returns time in milliseconds */
  47. static unsigned long long notrace tick_to_time(unsigned long long tick)
  48. {
  49. ulong div = get_tbclk();
  50. tick *= CONFIG_SYS_HZ;
  51. do_div(tick, div);
  52. return tick;
  53. }
  54. int __weak timer_init(void)
  55. {
  56. return 0;
  57. }
  58. /* Returns time in milliseconds */
  59. ulong __weak get_timer(ulong base)
  60. {
  61. return tick_to_time(get_ticks()) - base;
  62. }
  63. unsigned long __weak notrace timer_get_us(void)
  64. {
  65. return tick_to_time(get_ticks() * 1000);
  66. }
  67. static unsigned long long usec_to_tick(unsigned long usec)
  68. {
  69. unsigned long long tick = usec;
  70. tick *= get_tbclk();
  71. do_div(tick, 1000000);
  72. return tick;
  73. }
  74. void __weak __udelay(unsigned long usec)
  75. {
  76. unsigned long long tmp;
  77. tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
  78. while (get_ticks() < tmp+1) /* loop till event */
  79. /*NOP*/;
  80. }
  81. /* ------------------------------------------------------------------------- */
  82. void udelay(unsigned long usec)
  83. {
  84. ulong kv;
  85. do {
  86. WATCHDOG_RESET();
  87. kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
  88. __udelay (kv);
  89. usec -= kv;
  90. } while(usec);
  91. }
  92. void mdelay(unsigned long msec)
  93. {
  94. while (msec--)
  95. udelay(1000);
  96. }