generic_timer.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * (C) Copyright 2013
  3. * David Feng <fenghua@phytium.com.cn>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <asm/system.h>
  10. /*
  11. * Generic timer implementation of get_tbclk()
  12. */
  13. unsigned long get_tbclk(void)
  14. {
  15. unsigned long cntfrq;
  16. asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq));
  17. return cntfrq;
  18. }
  19. /*
  20. * Generic timer implementation of timer_read_counter()
  21. */
  22. unsigned long timer_read_counter(void)
  23. {
  24. unsigned long cntpct;
  25. #ifdef CONFIG_SYS_FSL_ERRATUM_A008585
  26. /* This erratum number needs to be confirmed to match ARM document */
  27. unsigned long temp;
  28. #endif
  29. isb();
  30. asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
  31. #ifdef CONFIG_SYS_FSL_ERRATUM_A008585
  32. asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
  33. while (temp != cntpct) {
  34. asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
  35. asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
  36. }
  37. #endif
  38. return cntpct;
  39. }
  40. unsigned long usec2ticks(unsigned long usec)
  41. {
  42. ulong ticks;
  43. if (usec < 1000)
  44. ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
  45. else
  46. ticks = ((usec / 10) * (get_tbclk() / 100000));
  47. return ticks;
  48. }