time.c 2.4 KB

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