timer.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2012 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
  4. * (C) Copyright 2012 Renesas Solutions Corp.
  5. */
  6. #include <common.h>
  7. #include <div64.h>
  8. #include <asm/io.h>
  9. #include <asm/arch-armv7/globaltimer.h>
  10. #include <asm/arch/rmobile.h>
  11. static struct globaltimer *global_timer = \
  12. (struct globaltimer *)GLOBAL_TIMER_BASE_ADDR;
  13. #define CLK2MHZ(clk) (clk / 1000 / 1000)
  14. static u64 get_cpu_global_timer(void)
  15. {
  16. u32 low, high;
  17. u64 timer;
  18. u32 old = readl(&global_timer->cnt_h);
  19. while (1) {
  20. low = readl(&global_timer->cnt_l);
  21. high = readl(&global_timer->cnt_h);
  22. if (old == high)
  23. break;
  24. else
  25. old = high;
  26. }
  27. timer = high;
  28. return (u64)((timer << 32) | low);
  29. }
  30. static u64 get_time_us(void)
  31. {
  32. u64 timer = get_cpu_global_timer();
  33. timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1));
  34. do_div(timer, CLK2MHZ(CONFIG_SYS_CPU_CLK));
  35. return timer;
  36. }
  37. static ulong get_time_ms(void)
  38. {
  39. u64 us = get_time_us();
  40. do_div(us, 1000);
  41. return us;
  42. }
  43. int timer_init(void)
  44. {
  45. writel(0x01, &global_timer->ctl);
  46. return 0;
  47. }
  48. void __udelay(unsigned long usec)
  49. {
  50. u64 start, current;
  51. u64 wait;
  52. start = get_cpu_global_timer();
  53. wait = (u64)((usec * CLK2MHZ(CONFIG_SYS_CPU_CLK)) >> 2);
  54. do {
  55. current = get_cpu_global_timer();
  56. } while ((current - start) < wait);
  57. }
  58. ulong get_timer(ulong base)
  59. {
  60. return get_time_ms() - base;
  61. }
  62. unsigned long long get_ticks(void)
  63. {
  64. return get_cpu_global_timer();
  65. }
  66. ulong get_tbclk(void)
  67. {
  68. return (ulong)(CONFIG_SYS_CPU_CLK >> 2);
  69. }