time.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 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(unsigned long long tick)
  46. {
  47. unsigned int div = get_tbclk();
  48. tick *= CONFIG_SYS_HZ;
  49. do_div(tick, div);
  50. return tick;
  51. }
  52. ulong __weak get_timer(ulong base)
  53. {
  54. return tick_to_time(get_ticks()) - base;
  55. }
  56. unsigned long __weak notrace timer_get_us(void)
  57. {
  58. return tick_to_time(get_ticks() * 1000);
  59. }
  60. static unsigned long long usec_to_tick(unsigned long usec)
  61. {
  62. unsigned long long tick = usec * get_tbclk();
  63. usec *= get_tbclk();
  64. do_div(tick, 1000000);
  65. return tick;
  66. }
  67. void __weak __udelay(unsigned long usec)
  68. {
  69. unsigned long long tmp;
  70. ulong tmo;
  71. tmo = usec_to_tick(usec);
  72. tmp = get_ticks() + tmo; /* get current timestamp */
  73. while (get_ticks() < tmp) /* loop till event */
  74. /*NOP*/;
  75. }
  76. /* ------------------------------------------------------------------------- */
  77. void udelay(unsigned long usec)
  78. {
  79. ulong kv;
  80. do {
  81. WATCHDOG_RESET();
  82. kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
  83. __udelay (kv);
  84. usec -= kv;
  85. } while(usec);
  86. }
  87. void mdelay(unsigned long msec)
  88. {
  89. while (msec--)
  90. udelay(1000);
  91. }