time.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 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. #else
  34. extern unsigned long __weak timer_read_counter(void);
  35. #endif
  36. unsigned long long __weak notrace get_ticks(void)
  37. {
  38. unsigned long now = timer_read_counter();
  39. /* increment tbu if tbl has rolled over */
  40. if (now < gd->timebase_l)
  41. gd->timebase_h++;
  42. gd->timebase_l = now;
  43. return ((unsigned long long)gd->timebase_h << 32) | gd->timebase_l;
  44. }
  45. static unsigned long long notrace tick_to_time(uint64_t tick)
  46. {
  47. unsigned int div = get_tbclk();
  48. tick *= CONFIG_SYS_HZ;
  49. do_div(tick, div);
  50. return tick;
  51. }
  52. int __weak timer_init(void)
  53. {
  54. return 0;
  55. }
  56. ulong __weak get_timer(ulong base)
  57. {
  58. return tick_to_time(get_ticks()) - base;
  59. }
  60. unsigned long __weak notrace timer_get_us(void)
  61. {
  62. return tick_to_time(get_ticks() * 1000);
  63. }
  64. static unsigned long long usec_to_tick(unsigned long usec)
  65. {
  66. uint64_t tick = usec;
  67. tick *= get_tbclk();
  68. do_div(tick, 1000000);
  69. return tick;
  70. }
  71. void __weak __udelay(unsigned long usec)
  72. {
  73. unsigned long long tmp;
  74. ulong tmo;
  75. tmo = usec_to_tick(usec);
  76. tmp = get_ticks() + tmo; /* get current timestamp */
  77. while (get_ticks() < tmp) /* loop till event */
  78. /*NOP*/;
  79. }
  80. /* ------------------------------------------------------------------------- */
  81. void udelay(unsigned long usec)
  82. {
  83. ulong kv;
  84. do {
  85. WATCHDOG_RESET();
  86. kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
  87. __udelay (kv);
  88. usec -= kv;
  89. } while(usec);
  90. }
  91. void mdelay(unsigned long msec)
  92. {
  93. while (msec--)
  94. udelay(1000);
  95. }