time.c 2.5 KB

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