time.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2008 - 2013 Tensilica Inc.
  4. */
  5. #include <common.h>
  6. #include <time.h>
  7. #include <asm/global_data.h>
  8. #include <linux/stringify.h>
  9. DECLARE_GLOBAL_DATA_PTR;
  10. #if XCHAL_HAVE_CCOUNT
  11. static ulong get_ccount(void)
  12. {
  13. ulong ccount;
  14. asm volatile ("rsr %0,"__stringify(CCOUNT) : "=a" (ccount));
  15. return ccount;
  16. }
  17. #else
  18. static ulong fake_ccount;
  19. #define get_ccount() fake_ccount
  20. #endif
  21. static void delay_cycles(unsigned cycles)
  22. {
  23. #if XCHAL_HAVE_CCOUNT
  24. unsigned expiry = get_ccount() + cycles;
  25. while ((signed)(expiry - get_ccount()) > 0)
  26. ;
  27. #else
  28. #warning "Without Xtensa timer option, timing will not be accurate."
  29. /*
  30. * Approximate the cycle count by a loop iteration count.
  31. * This is highly dependent on config and optimization.
  32. */
  33. volatile unsigned i;
  34. for (i = cycles >> 4U; i > 0; --i)
  35. ;
  36. fake_ccount += cycles;
  37. #endif
  38. }
  39. /*
  40. * Delay (busy-wait) for a number of microseconds.
  41. */
  42. void __udelay(unsigned long usec)
  43. {
  44. ulong lo, hi, i;
  45. ulong mhz = CONFIG_SYS_CLK_FREQ / 1000000;
  46. /* Scale to support full 32-bit usec range */
  47. lo = usec & ((1<<22)-1);
  48. hi = usec >> 22UL;
  49. for (i = 0; i < hi; ++i)
  50. delay_cycles(mhz << 22);
  51. delay_cycles(mhz * lo);
  52. }
  53. /*
  54. * Return the elapsed time (ticks) since 'base'.
  55. */
  56. ulong get_timer(ulong base)
  57. {
  58. /* Don't tie up a timer; use cycle counter if available (or fake it) */
  59. #if XCHAL_HAVE_CCOUNT
  60. register ulong ccount;
  61. __asm__ volatile ("rsr %0, CCOUNT" : "=a"(ccount));
  62. return ccount / (CONFIG_SYS_CLK_FREQ / CONFIG_SYS_HZ) - base;
  63. #else
  64. /*
  65. * Add at least the overhead of this call (in cycles).
  66. * Avoids hanging in case caller doesn't use udelay().
  67. * Note that functions that don't call udelay() (such as
  68. * the "sleep" command) will not get a significant delay
  69. * because there is no time reference.
  70. */
  71. fake_ccount += 20;
  72. return fake_ccount / (CONFIG_SYS_CLK_FREQ / CONFIG_SYS_HZ) - base;
  73. #endif
  74. }
  75. /*
  76. * This function is derived from ARM/PowerPC code (read timebase as long long).
  77. * On Xtensa it just returns the timer value.
  78. */
  79. unsigned long long get_ticks(void)
  80. {
  81. return get_timer(0);
  82. }
  83. /*
  84. * This function is derived from ARM/PowerPC code (timebase clock frequency).
  85. * On Xtensa it returns the number of timer ticks per second.
  86. */
  87. ulong get_tbclk(void)
  88. {
  89. return CONFIG_SYS_HZ;
  90. }
  91. #if XCHAL_HAVE_CCOUNT
  92. unsigned long timer_get_us(void)
  93. {
  94. unsigned long ccount;
  95. __asm__ volatile ("rsr %0, CCOUNT" : "=a"(ccount));
  96. return ccount / (CONFIG_SYS_CLK_FREQ / 1000000);
  97. }
  98. #endif