time.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #ifndef CONFIG_WD_PERIOD
  12. # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
  13. #endif
  14. DECLARE_GLOBAL_DATA_PTR;
  15. #ifdef CONFIG_SYS_TIMER_RATE
  16. /* Returns tick rate in ticks per second */
  17. ulong notrace get_tbclk(void)
  18. {
  19. return CONFIG_SYS_TIMER_RATE;
  20. }
  21. #endif
  22. #ifdef CONFIG_SYS_TIMER_COUNTER
  23. unsigned long notrace timer_read_counter(void)
  24. {
  25. #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
  26. return ~readl(CONFIG_SYS_TIMER_COUNTER);
  27. #else
  28. return readl(CONFIG_SYS_TIMER_COUNTER);
  29. #endif
  30. }
  31. #else
  32. extern unsigned long __weak timer_read_counter(void);
  33. #endif
  34. uint64_t __weak notrace get_ticks(void)
  35. {
  36. unsigned long now = timer_read_counter();
  37. /* increment tbu if tbl has rolled over */
  38. if (now < gd->timebase_l)
  39. gd->timebase_h++;
  40. gd->timebase_l = now;
  41. return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
  42. }
  43. /* Returns time in milliseconds */
  44. static uint64_t notrace tick_to_time(uint64_t tick)
  45. {
  46. ulong div = get_tbclk();
  47. tick *= CONFIG_SYS_HZ;
  48. do_div(tick, div);
  49. return tick;
  50. }
  51. int __weak timer_init(void)
  52. {
  53. return 0;
  54. }
  55. /* Returns time in milliseconds */
  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 uint64_t 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. uint64_t tmp;
  74. tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
  75. while (get_ticks() < tmp+1) /* loop till event */
  76. /*NOP*/;
  77. }
  78. /* ------------------------------------------------------------------------- */
  79. void udelay(unsigned long usec)
  80. {
  81. ulong kv;
  82. do {
  83. WATCHDOG_RESET();
  84. kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
  85. __udelay (kv);
  86. usec -= kv;
  87. } while(usec);
  88. }
  89. void mdelay(unsigned long msec)
  90. {
  91. while (msec--)
  92. udelay(1000);
  93. }