generic_timer.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. DECLARE_GLOBAL_DATA_PTR;
  11. /*
  12. * Generic timer implementation of get_tbclk()
  13. */
  14. unsigned long get_tbclk(void)
  15. {
  16. unsigned long cntfrq;
  17. asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq));
  18. return cntfrq;
  19. }
  20. /*
  21. * Generic timer implementation of timer_read_counter()
  22. */
  23. unsigned long timer_read_counter(void)
  24. {
  25. unsigned long cntpct;
  26. #ifdef CONFIG_SYS_FSL_ERRATUM_A008585
  27. /* This erratum number needs to be confirmed to match ARM document */
  28. unsigned long temp;
  29. #endif
  30. isb();
  31. asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
  32. #ifdef CONFIG_SYS_FSL_ERRATUM_A008585
  33. asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
  34. while (temp != cntpct) {
  35. asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
  36. asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
  37. }
  38. #endif
  39. return cntpct;
  40. }
  41. uint64_t get_ticks(void)
  42. {
  43. unsigned long ticks = timer_read_counter();
  44. gd->arch.tbl = ticks;
  45. return ticks;
  46. }
  47. unsigned long usec2ticks(unsigned long usec)
  48. {
  49. ulong ticks;
  50. if (usec < 1000)
  51. ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
  52. else
  53. ticks = ((usec / 10) * (get_tbclk() / 100000));
  54. return ticks;
  55. }