generic_timer.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2013
  4. * David Feng <fenghua@phytium.com.cn>
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <asm/system.h>
  9. DECLARE_GLOBAL_DATA_PTR;
  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. uint64_t get_ticks(void)
  41. {
  42. unsigned long ticks = timer_read_counter();
  43. gd->arch.tbl = ticks;
  44. return ticks;
  45. }
  46. unsigned long usec2ticks(unsigned long usec)
  47. {
  48. ulong ticks;
  49. if (usec < 1000)
  50. ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
  51. else
  52. ticks = ((usec / 10) * (get_tbclk() / 100000));
  53. return ticks;
  54. }
  55. ulong timer_get_boot_us(void)
  56. {
  57. u64 val = get_ticks() * 1000000;
  58. return val / get_tbclk();
  59. }